package com.jhhc.utils;


import com.jhhc.BDMS.Data.Dao.BDMS_ST_PPTN_EMapper;
import com.jhhc.BDMS.Data.Dao.BDMS_ST_RIVER_EMapper;
import com.jhhc.BDMS.Data.Dao.BDMS_ST_RSVR_EMapper;
import com.jhhc.BDMS.Data.Dao.BDMS_ST_ZVARL_BMapper;
import com.jhhc.BDMS.Data.Pojo.BDMS_ST_PPTN_E;
import com.jhhc.BDMS.Data.Pojo.BDMS_ST_RIVER_E;
import com.jhhc.BDMS.Data.Pojo.BDMS_ST_RSVR_E;
import com.jhhc.BDMS.Data.Pojo.BDMS_ST_ZVARL_B;
import com.jhhc.BDMS.Data.Service.BDMS_ST_ZVARL_BService;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_MONITORITEM_BService;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_SENSORTYPE_BService;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_SENSOR_BService;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_STBPRP_BService;
import com.jhhc.RHDB.Data.Pojo.*;

import com.jhhc.RHDB.Data.Service.*;
import com.jhhc.FloodForecast.Sensor.Pojo.*;
import com.jhhc.RHDB.Data.Pojo.ST_SED_R;
import com.jhhc.RHDB.Data.Pojo.ST_TIDEWAY_R;
import com.jhhc.RHDB.Data.Pojo.ST_WAVE_R;
import com.jhhc.RHDB.Data.Pojo.ST_WIND_R;
import com.jhhc.RHDB.Data.Service.*;
import com.jhhc.WaterQuality.Calculate.Forecast.entity.WQ_AWQMD_D;
import com.jhhc.WaterQuality.Calculate.Forecast.service.WQ_AWQMD_DService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class GetDataService {

    @Value("${datasourceType}")
    String datasourceType;

    @Autowired
    private ST_PPTN_RService ST_PPTN_RService;
    @Autowired
    private ST_RIVER_RService ST_RIVER_RService;
    @Autowired
    private ST_RSVR_RService ST_RSVR_RService;
    @Autowired
    private ST_PPTN_R5Service ST_PPTN_R5Service;
    @Autowired
    private ST_RIVER_R5Service ST_RIVER_R5Service;
    @Autowired
    private ST_RSVR_R5Service ST_RSVR_R5Service;
    @Autowired
    private ST_TIDE_RService ST_TIDE_RService;
    @Autowired
    private ST_FORECASTC_FService ST_FORECASTC_FService;
    @Autowired
    private ST_FORECAST_FService ST_FORECAST_FService;
    @Autowired
    private ST_WAS_RService ST_WAS_RService;
    @Autowired
    private ST_PUMP_RService ST_PUMP_RService;
    @Autowired
    private com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_SENSORALARM_BService BDMS_ST_SENSORALARM_BService;
    @Autowired
    private BDMS_ST_SENSOR_BService bDMS_ST_SENSOR_BService;
    @Autowired
    private ST_SED_RService st_sed_rService;
    @Autowired
    private ST_TIDEWAY_RService st_tideway_rService;
    @Resource
    private BDMS_ST_ZVARL_BMapper bdms_st_zvarl_bMapper;
    @Autowired
    private ST_WAVE_RService st_wave_rService;
    @Autowired
    private ST_WIND_RService st_wind_rService;
    @Autowired
    private WQ_AWQMD_DService wq_awqmd_dService;
    @Autowired
    private BDMS_ST_STBPRP_BService bdms_st_stbprp_bService;
    @Autowired
    private BDMS_ST_SENSOR_BService bdms_st_sensor_bService;
    @Autowired
    private ST_TIDE_HService st_tide_hService;
    @Autowired
    private BDMS_ST_MONITORITEM_BService bdms_st_monitoritem_bService;
    @Autowired
    private BDMS_ST_SENSORTYPE_BService bdms_st_sensortype_bService;

    @Resource
    private BDMS_ST_RSVR_EMapper bdms_st_rsvr_eMapper;
    @Resource
    private BDMS_ST_PPTN_EMapper bdms_st_pptn_eMapper;
    @Resource
    private BDMS_ST_RIVER_EMapper bdms_st_river_eMapper;



    //查询实况数据
    public List<Sensors> getdata(String sensorType, List<String> dataCodes, String startTM, String endTM,
                                 StepLength staticalDataType,int pid) {
        List<Sensors> sensorsList = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        switch (sensorType) {
            case "P":
                //查询更新表
                List<BDMS_ST_PPTN_E> pptn_es = bdms_st_pptn_eMapper.selectByPid(pid,dataCodes,startTM,endTM);
                System.out.println(pptn_es);
                List<ST_PPTN_R> ST_PPTN_RLists = new ArrayList<>();
                List<ST_PPTN_R>  stPPTNRs = null;
                switch (staticalDataType) {
                    case FiveMinute:
                        ST_PPTN_RLists = ST_PPTN_R5Service.selectByStationArrys(dataCodes, startTM, endTM);
                        break;
                    case Hour:
                    case Day:
                        ST_PPTN_RLists = ST_PPTN_RService.selectByStationArrys(dataCodes, startTM, endTM);
                        break;
                    default:
                        break;
                }

                stPPTNRs  =ST_PPTN_RService.selectPPTNNewEstData(dataCodes);


                if (staticalDataType == StepLength.Day) { // 日雨量
                    if (!ST_PPTN_RLists.isEmpty()){
                        Map<String, List<ST_PPTN_R>> PPTNListGroup = ST_PPTN_RLists.stream().collect(Collectors.groupingBy(ST_PPTN_R::getStcd, Collectors.toList()));
                        for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
                            Sensors sensors = new Sensors();
                            sensors.setDatacode(PPTNList.getKey());
                            for (int i = 0; i < PPTNList.getValue().size(); i++) {
                                Data data =  new Data();
                                if (PPTNList.getValue().get(i).getDyp() != null) {
                                    data.setTM(PPTNList.getValue().get(i).getTm());
                                    data.setValue(PPTNList.getValue().get(i).getDyp());
                                    sensors.setDataList(data );
                                    sensors.setSensorType(SensorType.P);
                                    sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM().toString());
                                    sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM().toString());
                                    sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                                    sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                                    sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                                    sensors.setTotalValue(sensors.getDataList().stream().mapToDouble(Data::getValue).sum());
                                    ST_PPTN_R st_tide_r= stPPTNRs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(PPTNList.getKey())).findAny().orElse(null);
                                    if (st_tide_r!=null) {
                                        String endTime = st_tide_r.getTm().substring(0, 19);
                                        sensors.setRemark("此站点改传感器最新数据时间点为：" + endTime);
                                    }
                                }
                            }

                            sensorsList.add(sensors);
                        }
                    }else {
                        Map<String, List<ST_PPTN_R>> PPTNListGroup = stPPTNRs.stream().collect(Collectors.groupingBy(ST_PPTN_R::getStcd, Collectors.toList()));
                        for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
                            Sensors sensors = new Sensors();
                            sensors.setDatacode(PPTNList.getKey());
                            sensors.setSensorType(SensorType.P);
                            ST_PPTN_R st_tide_r= stPPTNRs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(PPTNList.getKey())).findAny().orElse(null);
                            if (st_tide_r!=null){
                                String endTime=st_tide_r.getTm().substring(0,19);
                                try {
                                    Date end =  format.parse(endTime);
                                    calendar.setTime(end);
                                    calendar.add(Calendar.DATE, -3);
                                }catch (Exception ignored){

                                }
                                sensors.setDataStartTime(format.format(calendar.getTime()));
                                sensors.setDataEndTime(endTime);
                            }
                            sensorsList.add(sensors);
                        }
                    }

                } else {
                    if (!ST_PPTN_RLists.isEmpty()) {
                        Map<String, List<ST_PPTN_R>> PPTNListGroup = ST_PPTN_RLists.stream().collect(Collectors.groupingBy(ST_PPTN_R::getStcd, Collectors.toList()));
                        for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
                            Sensors sensors = new Sensors();
                            List<Data> datas = new ArrayList<>();
                            sensors.setDatacode(PPTNList.getKey());
                            Double addDrp = 0.0;
                            List<BDMS_ST_PPTN_E> pptnList =pptn_es.stream().filter(p -> p.getStcd().trim().equals(PPTNList.getKey())).collect(Collectors.toList());
                            for (int i = 0; i < PPTNList.getValue().size(); i++) {
                                Data data = new Data();
                                if (PPTNList.getValue().get(i).getDrp() != null) {
                                    int finalI = i;
                                    BDMS_ST_PPTN_E bdms_st_pptn_e = pptnList.stream().filter(pptn->pptn.getTm().equals(PPTNList.getValue().get(finalI).getTm())).findAny().orElse(null);
                                    if (bdms_st_pptn_e!= null){
                                        if (bdms_st_pptn_e.getDrp() != null){
                                            data.setValue(bdms_st_pptn_e.getDrp());
                                            data.setFlag(1);
                                        }
                                    }else {
                                        data.setValue(PPTNList.getValue().get(i).getDrp());
                                    }
                                    data.setTM(PPTNList.getValue().get(i).getTm());
                                    sensors.setDataList(data);
                                    sensors.setSensorType(SensorType.P);
                                    sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                                    sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                                    sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                                    sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                                    sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                                    sensors.setTotalValue(sensors.getDataList().stream().mapToDouble(Data::getValue).sum());
                                    ST_PPTN_R st_tide_r= stPPTNRs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(PPTNList.getKey())).findAny().orElse(null);
                                    if (st_tide_r!=null) {
                                        String endTime = st_tide_r.getTm().substring(0, 19);
                                        sensors.setRemark("此站点改传感器最新数据时间点为：" + endTime);
                                    }
                                }
                                if (i != PPTNList.getValue().size()) {
                                    if (PPTNList != null && PPTNList.getValue().get(i) != null && PPTNList.getValue().get(i).getDrp() != null) {
                                        Data data1 = new Data();
                                        addDrp += data.getValue();
                                        data1.setTM(PPTNList.getValue().get(i).getTm());
                                        data1.setValue(addDrp);
                                        datas.add(data1);
                                        sensors.setHourlyStatisticsDatas(datas);
                                    }

                                }
                                sensorsList.add(sensors);
                            }
                        }
                    } else {
                        Map<String, List<ST_PPTN_R>> PPTNListGroup = stPPTNRs.stream().collect(Collectors.groupingBy(ST_PPTN_R::getStcd, Collectors.toList()));
                        for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
                            Sensors sensors = new Sensors();
                            sensors.setDatacode(PPTNList.getKey());
                            sensors.setSensorType(SensorType.P);
                            ST_PPTN_R st_tide_r= stPPTNRs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(PPTNList.getKey())).findAny().orElse(null);
                            if (st_tide_r!=null){
                                String endTime=st_tide_r.getTm().substring(0,19);
                                sensors.setRemark("此站点最新数据时间点为："+endTime);
                                try {
                                    Date end =  format.parse(endTime);
                                    calendar.setTime(end);
                                    calendar.add(Calendar.DATE, -3);
                                }catch (Exception ignored){

                                }
                                sensors.setDataStartTime(format.format(calendar.getTime()));
                                sensors.setDataEndTime(endTime);
                            }
                            sensorsList.add(sensors);
                        }
                    }
                }
                break;
            case "Z":
            case "Q":
                //查询更新表
                List<BDMS_ST_RIVER_E> river_es = bdms_st_river_eMapper.selectByPid(pid,dataCodes,startTM,endTM);
                List<ST_RIVER_R> ST_RIVER_RList = new ArrayList<>();
                List<ST_RIVER_R> ST_RIVER_RS;
                ST_RIVER_RS  =ST_RIVER_RService.selectRIVERNewEstData(dataCodes);
              //  ST_RIVER_R st_river_r = null;
                switch (staticalDataType) {// 判断连接表服务
                    case FiveMinute:
                       if( ST_RIVER_R5Service.selectByStationArrys(dataCodes, startTM, endTM)!=null){
                           ST_RIVER_RList = ST_RIVER_R5Service.selectByStationArrys(dataCodes, startTM, endTM);
                       }
                        break;
                    case Hour:
                        ST_RIVER_RList = ST_RIVER_RService.selectByStationArrys(dataCodes, startTM, endTM);
                        break;
                    default:
                        break;
                }
                if (ST_RIVER_RList!=null&& !ST_RIVER_RList.isEmpty()){
                    Map<String, List<ST_RIVER_R>> RIVERListGroup = ST_RIVER_RList.stream()
                            .collect(Collectors.groupingBy(ST_RIVER_R::getStcd, Collectors.toList()));
                    for (Map.Entry<String, List<ST_RIVER_R>> RIVERList : RIVERListGroup.entrySet()) {
                        Sensors sensors = new Sensors();
                        //sensors.setSensorType(sensorType);
                        sensors.setDatacode(RIVERList.getKey());
                        List<BDMS_ST_RIVER_E> riverList =river_es.stream().filter(p -> p.getStcd().trim().equals(RIVERList.getKey())).collect(Collectors.toList());
                        for (int i = 0; i < RIVERList.getValue().size(); i++) {
                            int finalI = i;
                            BDMS_ST_RIVER_E bdms_st_river_e = riverList.stream().filter(pptn->pptn.getTm().equals(RIVERList.getValue().get(finalI).getTm())).findAny().orElse(null);
                            Data d = new Data();
                            d.setTM(RIVERList.getValue().get(i).getTm());
                            if ("Q".equals(sensorType)) {// 流量
                                if (RIVERList.getValue().get(i).getQ() != null) {
                                    if (bdms_st_river_e!= null) {
                                        if (bdms_st_river_e.getQ() != null) {
                                            d.setValue(bdms_st_river_e.getQ());
                                            d.setFlag(1);
                                        }
                                    }else {
                                        d.setValue(RIVERList.getValue().get(i).getQ());
                                    }
                                }
                                sensors.setDataList(d);
                                sensors.setSensorType(SensorType.Q);
                            } else {//水位
                                if (RIVERList.getValue().get(i).getZ() != null) {
                                    if (bdms_st_river_e!= null){
                                        if (bdms_st_river_e.getZ() != null){
                                            d.setValue(bdms_st_river_e.getZ());
                                            d.setFlag(1);
                                        }
                                    }else {
                                        d.setValue(RIVERList.getValue().get(i).getZ());
                                    }
                                }
                                sensors.setDataList(d);
                                sensors.setSensorType(SensorType.Z);
                            }
                            ST_RIVER_R st_tide_r= ST_RIVER_RS.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(RIVERList.getKey())).findAny().orElse(null);
                            if (st_tide_r!=null) {
                                String endTime = st_tide_r.getTm().substring(0, 19);
                                sensors.setRemark("此站点最新数据时间点为："+endTime);
                            }
                            if (sensors.getDataList() != null) {
                                sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM().toString());
                                sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM().toString());
                                sensors.setMinData(sensors.getDataList().stream()
                                        .filter(data -> data.getValue() != null)
                                        .min(Comparator.comparing(Data::getValue))
                                        .orElse(null));

                                sensors.setMaxData(sensors.getDataList().stream()
                                        .filter(data -> data.getValue() != null)
                                        .max(Comparator.comparing(Data::getValue))
                                        .orElse(null));
//                                sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                                if (sensors == null) {
                                    throw new IllegalArgumentException("Sensors object is null");
                                }

                                List<Data> dataList = sensors.getDataList();
                                if (dataList == null) {
                                    throw new IllegalArgumentException("Data list is null");
                                }

                                double avgValue = dataList.stream()
                                        .filter(Objects::nonNull) // 过滤掉 null 的 Data 对象
                                        .filter(data -> data.getValue() != null)
                                        .mapToDouble(data -> {
                                            return data.getValue();
                                        })
                                        .average()
                                        .orElse(0.0); // 如果没有元素，返回默认值 0.0

                                sensors.setAvgValue(avgValue);


                            }
                        }
                        sensorsList.add(sensors);
                    }
                }else {
                    //获取最新数据返回
                    Map<String, List<ST_RIVER_R>> PPTNListGroup = ST_RIVER_RS.stream().collect(Collectors.groupingBy(ST_RIVER_R::getStcd, Collectors.toList()));
                    for (Map.Entry<String, List<ST_RIVER_R>> PPTNList : PPTNListGroup.entrySet()) {
                        Sensors sensors = new Sensors();
                        sensors.setDatacode(PPTNList.getKey());
                            switch (sensorType) {
                                case "Z":
                                    sensors.setSensorType(SensorType.Z);
                                    break;
                                case "Q":
                                    sensors.setSensorType(SensorType.Q);
                                    break;
                                default:
                                    break;
                            }
                            sensors.setSensorType(SensorType.Z);
                            ST_RIVER_R st_tide_r= ST_RIVER_RS.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(PPTNList.getKey())).findAny().orElse(null);
                            if (st_tide_r!=null){
                                String endTime=st_tide_r.getTm().substring(0,19);
                                try {
                                    Date end =  format.parse(endTime);
                                    calendar.setTime(end);
                                    calendar.add(Calendar.DATE, -3);
                                }catch (Exception ignored){
                                }
                                sensors.setDataStartTime(format.format(calendar.getTime()));
                                sensors.setDataEndTime(endTime);
                            }
                        sensorsList.add(sensors);
                    }
                }
                break;
            case "INQ":
            case "OTQ":
            case "RZ":
            case "W":
                //查询更新表
                List<BDMS_ST_RSVR_E> rsvr_es = bdms_st_rsvr_eMapper.selectByPid(pid,dataCodes,startTM,endTM);
                //查询库容关系曲线
                List<BDMS_ST_ZVARL_B> bdms_st_zvarl_bs = bdms_st_zvarl_bMapper.selects(pid, dataCodes);

                List<ST_RSVR_R> ST_RSVR_RList = new ArrayList<ST_RSVR_R>();
                List<ST_RSVR_R> st_rsvr_rs = null;
                st_rsvr_rs = ST_RSVR_RService.selectRSVRNewEstData(dataCodes);
                switch (staticalDataType) {
                    case FiveMinute:
                        ST_RSVR_RList = ST_RSVR_R5Service.selectByStationArrys(dataCodes, startTM, endTM);
                        break;
                    case Day:
                        ST_RSVR_RList = ST_RSVR_RService.selectByStationArrys(dataCodes, startTM, endTM).stream().filter(ST_RSVR_R ->
                                "08".equals(ST_RSVR_R.getTm().substring(11,13))).collect(Collectors.toList());
                        break;
                    case Hour:
                        ST_RSVR_RList = ST_RSVR_RService.selectByStationArrys(dataCodes, startTM, endTM);
                        break;
                    default:
                        break;
                }

                if (!ST_RSVR_RList.isEmpty()){
                    Map<String, List<ST_RSVR_R>> RSVRListGroup = ST_RSVR_RList.stream()
                            .collect(Collectors.groupingBy(ST_RSVR_R::getStcd, Collectors.toList()));
                    for (Map.Entry<String, List<ST_RSVR_R>> RSVRList : RSVRListGroup.entrySet()) {
                        Sensors sensors = new Sensors();
                        sensors.setDatacode(RSVRList.getKey());
                        List<BDMS_ST_RSVR_E> rsvrList =rsvr_es.stream().filter(p -> p.getStcd().trim().equals(RSVRList.getKey())).collect(Collectors.toList());
                        for (int i = 0; i < RSVRList.getValue().size(); i++) {
                            int finalI = i;
                            BDMS_ST_RSVR_E bdms_st_rsvr_e = rsvrList.stream().filter(pptn->pptn.getTm().equals(RSVRList.getValue().get(finalI).getTm())).findAny().orElse(null);
                            Data data = new Data();
                            data.setTM(RSVRList.getValue().get(i).getTm());
                            switch (sensorType) {
                                case "INQ":
                                  if (RSVRList.getValue().get(i).getInq() != null) {// 入库流量

                                      if (bdms_st_rsvr_e!= null) {
                                          if (bdms_st_rsvr_e.getInq() != null) {
                                              data.setValue(bdms_st_rsvr_e.getInq());
                                              data.setFlag(1);
                                          }
                                      } else{
                                          data.setValue(RSVRList.getValue().get(i).getInq());
                                      }
                                  }
                                    sensors.setDataList(data);
                                    sensors.setSensorType(SensorType.INQ);
                                    break;
                                case "OTQ":
                                   if (RSVRList.getValue().get(i).getOtq() != null) {// 入库流量
                                       if (bdms_st_rsvr_e != null) {
                                           if (bdms_st_rsvr_e.getOtq() != null) {
                                               data.setValue(bdms_st_rsvr_e.getOtq());
                                               data.setFlag(1);
                                           }
                                       } else {
                                           data.setValue(RSVRList.getValue().get(i).getOtq());
                                       }
                                   }
                                    sensors.setDataList(data);
                                    sensors.setSensorType(SensorType.OTQ);
                                    break;
                                case "RZ":
                                    if (RSVRList.getValue().get(i).getRz() != null) {// 入库流量
                                        data.setValue(RSVRList.getValue().get(i).getRz());
                                    }
                                    if (bdms_st_rsvr_e != null) {
                                        if (bdms_st_rsvr_e.getRz() != null) {
                                            data.setValue(bdms_st_rsvr_e.getRz());
                                            data.setFlag(1);
                                        }
                                    }
                                    sensors.setDataList(data);
                                    sensors.setSensorType(SensorType.RZ);
                                    break;
                                case "W":
                                  //  System.out.println(RSVRList.getValue().get(i));
                                   if (RSVRList.getValue().get(i).getW() != null) {// 入库流量
                                       if (bdms_st_rsvr_e!= null) {
                                           if (bdms_st_rsvr_e.getRz() != null) {
                                              // data.setValue(bdms_st_rsvr_e.getW());
                                               data.setFlag(1);
                                               double rz = bdms_st_rsvr_e.getRz();

                                               ImageBase64 imageBase64 = new ImageBase64();
                                               // 调用水位关系曲线
                                               if (bdms_st_zvarl_bs!=null &&bdms_st_zvarl_bs.size()>0){
                                                   int front = 0;
                                                   int after = 0;
                                                   double x1 = 0;
                                                   double y1 = 0;
                                                   double x2 = 0;
                                                   double y2 = 0;
                                                   for (int k = 0; k < bdms_st_zvarl_bs.size(); k++) {
                                                       if (rz < bdms_st_zvarl_bs.get(k).getRz())  {
                                                           front = k - 1;
                                                           after = k;
                                                           break;
                                                       }
                                                   }
                                                   try {
                                                       if (bdms_st_zvarl_bs.get(front) != null) {
                                                           x1 = Double.parseDouble(imageBase64.doubleAccuracy2(bdms_st_zvarl_bs.get(front).getRz()));
                                                           y1 = Double.parseDouble(imageBase64.doubleAccuracy2(bdms_st_zvarl_bs.get(front).getW()));
                                                       }
                                                       if (bdms_st_zvarl_bs.get(after) != null) {
                                                           x2 = Double.parseDouble(imageBase64.doubleAccuracy2(bdms_st_zvarl_bs.get(after).getRz()));
                                                           y2 = Double.parseDouble(imageBase64.doubleAccuracy2(bdms_st_zvarl_bs.get(after).getW()));
                                                       }
                                                       double w = imageBase64.W(x1, y1, x2, y2, rz);

                                                       data.setValue(w);
                                                   }catch (Exception e){
                                                       continue;
                                                   }
                                               }
                                           }
                                       } else {
                                           data.setValue(RSVRList.getValue().get(i).getW());
                                       }
                                    } else if (RSVRList.getValue().get(i).getRz()!=null){
                                        // 库水位
                                        double rz = RSVRList.getValue().get(i).getRz();

                                        ImageBase64 imageBase64 = new ImageBase64();
                                        // 调用水位关系曲线
                                        if (bdms_st_zvarl_bs!=null &&bdms_st_zvarl_bs.size()>0){
                                            int front = 0;
                                            int after = 0;
                                            double x1 = 0;
                                            double y1 = 0;
                                            double x2 = 0;
                                            double y2 = 0;
                                            for (int k = 0; k < bdms_st_zvarl_bs.size(); k++) {
                                                if (rz < bdms_st_zvarl_bs.get(k).getRz())  {
                                                    front = k - 1;
                                                    after = k;
                                                    break;
                                                }
                                            }
                                            try {
                                                if (bdms_st_zvarl_bs.get(front) != null) {
                                                    x1 = Double.parseDouble(imageBase64.doubleAccuracy2(bdms_st_zvarl_bs.get(front).getRz()));
                                                    y1 = Double.parseDouble(imageBase64.doubleAccuracy2(bdms_st_zvarl_bs.get(front).getW()));
                                                }
                                                if (bdms_st_zvarl_bs.get(after) != null) {
                                                    x2 = Double.parseDouble(imageBase64.doubleAccuracy2(bdms_st_zvarl_bs.get(after).getRz()));
                                                    y2 = Double.parseDouble(imageBase64.doubleAccuracy2(bdms_st_zvarl_bs.get(after).getW()));
                                                }
                                                double w = imageBase64.W(x1, y1, x2, y2, rz);
                                                data.setValue(w);
                                            }catch (Exception e){
                                                continue;
                                            }
                                        }
                                    }
                                    sensors.setDataList(data);
                                    sensors.setSensorType(SensorType.W);
                                    break;
                                default:
                                    break;

                            }
                            ST_RSVR_R  st_tide_r = st_rsvr_rs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(RSVRList.getKey())).findAny().orElse(null);

                            String endTime = st_tide_r.getTm().substring(0,19);;
                            sensors.setRemark("此站点最新数据时间点为："+endTime);

                            if (sensors.getDataList() != null) {
                                sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                                sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                                sensors.setMinData(sensors.getDataList().stream()
                                        .filter(data2 -> data2.getValue() != null)
                                        .min(Comparator.comparing(Data::getValue))
                                        .orElse(null));

                                sensors.setMaxData(sensors.getDataList().stream()
                                        .filter(data2 -> data2.getValue() != null)
                                        .max(Comparator.comparing(Data::getValue))
                                        .orElse(null));
                                sensors.setAvgValue(sensors.getDataList().stream()
                                        .filter(dat-> dat.getValue() != null) // 过滤掉值为null的Data对象
                                        .mapToDouble(Data::getValue)
                                        .average()
                                        .orElse(0.0)); // 如果列表为空，返回默认值0.0


                            }
                        }

                        if  (sensors.getDataList()==null|| sensors.getDataList().isEmpty()) {
                            ST_RSVR_R st_tide_r = null;
                            if (st_rsvr_rs != null) {
                                st_tide_r = st_rsvr_rs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(RSVRList.getKey())).findAny().orElse(null);

                                String endTime = st_tide_r.getTm().substring(0,19);;
                                try {
                                    Date end = format.parse(endTime);
                                    calendar.setTime(end);
                                    calendar.add(Calendar.DATE, -3);
                                } catch (Exception ignored) {
                                }
                                sensors.setDataStartTime(format.format(calendar.getTime()));
                                sensors.setDataEndTime(endTime);
                            }
                        }
                        sensorsList.add(sensors);
                    }
                }else {
                    Map<String, List<ST_RSVR_R>> RSVRListGroup = st_rsvr_rs.stream()
                            .collect(Collectors.groupingBy(ST_RSVR_R::getStcd, Collectors.toList()));
                    for (Map.Entry<String, List<ST_RSVR_R>> RSVRList : RSVRListGroup.entrySet()) {
                        Sensors sensors = new Sensors();
                        sensors.setDatacode(RSVRList.getKey());
                        for (int i = 0; i < RSVRList.getValue().size(); i++) {
                            switch (sensorType) {
                                case "INQ":
                                    sensors.setSensorType(SensorType.INQ);
                                    break;
                                case "OTQ":
                                   
                                    sensors.setSensorType(SensorType.OTQ);
                                    break;
                                case "RZ":
                                    sensors.setSensorType(SensorType.RZ);
                                    break;
                                case "W":
                                    sensors.setSensorType(SensorType.W);
                                    break;
                                default:
                                    break;
                            }
                            ST_RSVR_R st_tide_r = st_rsvr_rs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(RSVRList.getKey())).findAny().orElse(null);
                            System.out.println(st_rsvr_rs);
                            if (st_tide_r != null) {
                                String endTime = st_tide_r.getTm().substring(0,19);;
                                try {
                                    Date end = format.parse(endTime);
                                    calendar.setTime(end);
                                    calendar.add(Calendar.DATE, -3);
                                } catch (Exception ignored) {
                                }
                                sensors.setDataStartTime(format.format(calendar.getTime()));
                                sensors.setDataEndTime(endTime);
                            }
                        }
                        sensorsList.add(sensors);
                    }
                }
                
                break;
            case "TDZ":
                List<ST_TIDE_R> ST_TIDE_RList = new ArrayList<ST_TIDE_R>();
                switch (staticalDataType) {// 判断连接表服务
                    case FiveMinute:
                        ST_TIDE_RList = ST_TIDE_RService.selectByStationArrys(dataCodes, startTM, endTM);
                        break;
                    case Hour:
                        ST_TIDE_RList = ST_TIDE_RService.selectByStcd(dataCodes, startTM, endTM);
                        break;
                    default:
                        break;
                }
                Map<String, List<ST_TIDE_R>> ST_TIDE_RListGROUP = ST_TIDE_RList.stream()
                        .collect(Collectors.groupingBy(ST_TIDE_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_TIDE_R>> TIDEList : ST_TIDE_RListGROUP.entrySet()) {
                    Sensors sensors = new Sensors();
                    sensors.setDatacode(TIDEList.getKey());
                    for (int i = 0; i < TIDEList.getValue().size(); i++) {
                            if (TIDEList.getValue().get(i).getTdz() != null) {
                                sensors.setDataList(
                                        new Data(TIDEList.getValue().get(i).getTm(), TIDEList.getValue().get(i).getTdz()));
                                sensors.setSensorType(SensorType.TDZ);
                            }else{
                                sensors.setSensorType(SensorType.TDZ);
                            }
                        }
                        if (sensors.getDataList() != null) {
                            sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                            sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                            sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                            sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                            sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                            sensors.setTotalValue(sensors.getDataList().stream().mapToDouble(Data::getValue).sum());
                        }
                    sensorsList.add(sensors);
                }
                break;

            //堰闸
            //总过闸流量 TGTQ,
            //闸下水位  DWZ,
            //闸上水位    UPZ
            case "UPZ":
            case "DWZ":
            case "TGTQ":
                List<ST_WAS_R> ST_WAS_RList = ST_WAS_RService.selectByStationArrys(dataCodes, startTM, endTM);
                Map<String, List<ST_WAS_R>> ST_WAS_RListGROUP = ST_WAS_RList.stream()
                        .collect(Collectors.groupingBy(ST_WAS_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_WAS_R>> WASList : ST_WAS_RListGROUP.entrySet()) {
                    Sensors sensors = new Sensors();
                    sensors.setDatacode(WASList.getKey());
                    for (int i = 0; i < WASList.getValue().size(); i++) {
                        switch (sensorType) {
                            case "UPZ":
                                if (WASList.getValue().get(i).getUpz() != null) {//  //闸上水位    UPZ
                                    sensors.setDataList(new Data(WASList.getValue().get(i).getTm(), WASList.getValue().get(i).getUpz()));
                                    sensors.setSensorType(SensorType.UPZ);
                                }
                                break;
                            case "DWZ":
                                if (WASList.getValue().get(i).getDwz() != null) {///闸下水位  DWZ,
                                    sensors.setDataList(new Data(WASList.getValue().get(i).getTm(), WASList.getValue().get(i).getDwz()));
                                    sensors.setSensorType(SensorType.DWZ);
                                }
                                break;
                            case "TGTQ":
                                if (WASList.getValue().get(i).getTgtq() != null) {//总过闸流量 TGTQ,
                                    sensors.setDataList(new Data(WASList.getValue().get(i).getTm(), WASList.getValue().get(i).getTgtq()));
                                    sensors.setSensorType(SensorType.TGTQ);
                                }
                                break;

                        }
                        if (sensors.getDataList() != null) {
                            sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                            sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                            sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                            sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                        }
                    }
                    sensorsList.add(sensors);
                }
                break;
            //泵站
            //PMPQ  抽水流量
            //PPUPZ 站上水位
            //PPDWZ 站下水位
            case "PMPQ":
            case "PPUPZ":
            case "PPDWZ":
                List<ST_PUMP_R> ST_PUMP_RList = ST_PUMP_RService.selectByStationArrys(dataCodes, startTM, endTM);
                Map<String, List<ST_PUMP_R>> ST_PUMP_RListGROUP = ST_PUMP_RList.stream()
                        .collect(Collectors.groupingBy(ST_PUMP_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_PUMP_R>> PUMPList : ST_PUMP_RListGROUP.entrySet()) {
                    Sensors sensors = new Sensors();
                    sensors.setDatacode(PUMPList.getKey());
                    for (int i = 0; i < PUMPList.getValue().size(); i++) {
                        switch (sensorType) {
                            case "PMPQ":
                                if (PUMPList.getValue().get(i).getPmpq() != null) {//  //PMPQ  抽水流量
                                    sensors.setDataList(new Data(PUMPList.getValue().get(i).getTm(), PUMPList.getValue().get(i).getPmpq()));
                                    sensors.setSensorType(SensorType.PMPQ);
                                }
                                break;
                            case "PPUPZ":
                                if (PUMPList.getValue().get(i).getPpupz() != null) {  //PPUPZ 站上水位
                                    sensors.setDataList(new Data(PUMPList.getValue().get(i).getTm(), PUMPList.getValue().get(i).getPpupz()));
                                    sensors.setSensorType(SensorType.PPUPZ);
                                }
                                break;
                            case "PPDWZ":
                                if (PUMPList.getValue().get(i).getPpdwz() != null) { //PPUWZ 站下水位
                                    sensors.setDataList(new Data(PUMPList.getValue().get(i).getTm(), PUMPList.getValue().get(i).getPpdwz()));
                                    sensors.setSensorType(SensorType.PPDWZ);
                                }
                                break;

                        }
                        if (sensors.getDataList() != null) {
                            sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                            sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                            sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                            sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                        }
                    }
                    sensorsList.add(sensors);
                }
                break;
            case "SED":
                List<ST_SED_R> ST_SED_RList = st_sed_rService.selectByStationArrys(dataCodes, startTM, endTM);
                Map<String, List<ST_SED_R>> ST_SED_RListGROUP = ST_SED_RList.stream()
                        .collect(Collectors.groupingBy(ST_SED_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_SED_R>> TIDEList : ST_SED_RListGROUP.entrySet()) {
                    Sensors sensors = new Sensors();
                    sensors.setDatacode(TIDEList.getKey());
                    for (int i = 0; i < TIDEList.getValue().size(); i++) {
                        if (TIDEList.getValue().get(i).getS() != null) {
                            sensors.setDataList(
                                    new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getS())));
                        }
                        sensors.setSensorType(SensorType.SED);
                    }
                    if (sensors.getDataList() != null) {
                        sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                        sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                        sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                        sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                        sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                        sensors.setTotalValue(sensors.getDataList().stream().mapToDouble(Data::getValue).sum());
                    }
                    sensorsList.add(sensors);
                }
                break;
            //波浪表
            case "AVWVHGT":
            case "AVWVHGTCY":
            case "WVDIR":
            case "WVHGT0103":
            case "WVHGT010":
            case "MWVHGT":
                List<ST_WAVE_R> ST_WAVE_RList = st_wave_rService.selectByStationArrys(dataCodes, startTM, endTM);
                Map<String, List<ST_WAVE_R>> ST_WAVE_RListGROUP = ST_WAVE_RList.stream()
                        .collect(Collectors.groupingBy(ST_WAVE_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_WAVE_R>> TIDEList : ST_WAVE_RListGROUP.entrySet()) {
                    Sensors sensors = new Sensors();
                    sensors.setDatacode(TIDEList.getKey());
                    for (int i = 0; i < TIDEList.getValue().size(); i++) {
                        switch (sensorType) {
                            case  "AVWVHGT":
                                if (TIDEList.getValue().get(i).getAvwvhgt() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAvwvhgt())));
                                }
                                sensors.setSensorType(SensorType.AVWVHGT);
                                break;
                            case  "AVWVHGTCY":
                                if (TIDEList.getValue().get(i).getAvwvhgt() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAvwvhgtcy())));
                                }
                                sensors.setSensorType(SensorType.AVWVHGTCY);
                                break;
                            case  "WVDIR":
                                if (TIDEList.getValue().get(i).getAvwvhgt() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getWvdir())));
                                }
                                sensors.setSensorType(SensorType.WVDIR);
                                break;
                            case  "WVHGT0103":
                                if (TIDEList.getValue().get(i).getWvhgt0103() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getWvhgt0103())));
                                }
                                sensors.setSensorType(SensorType.WVHGT0103);
                                break;
                            case  "WVHGT010":
                                if (TIDEList.getValue().get(i).getWvhgt010() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getWvhgt010())));
                                }
                                sensors.setSensorType(SensorType.WVHGT010);
                                break;
                            case  "MWVHGT":
                                if (TIDEList.getValue().get(i).getMwvhgt() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getMwvhgt())));
                                }
                                sensors.setSensorType(SensorType.MWVHGT);
                                break;
                        }
                    }
                    if (sensors.getDataList() != null) {
                        sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                        sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                        sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                        sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                    }
                    sensorsList.add(sensors);
                }

                break;
//潮流表
            case "V00H":
            case "DIR00H":
            case "V02H":
            case "DIR02H":
            case "V04H":
            case "DIR04H":
            case "V06H":
            case "DIR06H":
            case "V08H":
            case "DIR08H":
            case "V10H":
            case "DIR10H":
            case "AVV":
            case "AVDIR":
                List<ST_TIDEWAY_R> ST_TIDEWAY_RList = st_tideway_rService.selectByStationArrys(dataCodes, startTM, endTM);

                Map<String, List<ST_TIDEWAY_R>> ST_TIDEWAY_RListGROUP = ST_TIDEWAY_RList.stream()
                        .collect(Collectors.groupingBy(ST_TIDEWAY_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_TIDEWAY_R>> TIDEList : ST_TIDEWAY_RListGROUP.entrySet()) {
                    Sensors sensors = new Sensors();
                    sensors.setDatacode(TIDEList.getKey());
                    for (int i = 0; i < TIDEList.getValue().size(); i++) {
                        switch (sensorType) {
                            case  "V00H":
                                if (TIDEList.getValue().get(i).getV00h() != null) {
                                    sensors.setDataList(new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getV00h())));
                                    //流速
                                    if (Double.parseDouble(TIDEList.getValue().get(i).getDir00h())>180||
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir00h())<20) {//涨潮

                                        //获取涨潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir00h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfFloodTide(Double.valueOf(st_tideway_r.getV00h()));
                                        sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                                    }else if(Double.parseDouble(TIDEList.getValue().get(i).getDir00h())<180&&
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir00h())>20){
                                        //获取落潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir00h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfEbbTide(  Double.valueOf(st_tideway_r.getV00h()));
                                    }
                                }
                                sensors.setSensorType(SensorType.V00H);
                                break;
                            case  "DIR00H":
                                if (TIDEList.getValue().get(i).getDir00h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getDir00h())));
                                }
                                sensors.setSensorType(SensorType.DIR00H);
                                break;
                            case  "V02H":
                                if (TIDEList.getValue().get(i).getV02h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getV02h())));
                                    //流速

                                    if (Double.parseDouble(TIDEList.getValue().get(i).getDir02h())>180||
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir02h())<20) {//涨潮
                                        //获取涨潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir02h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfFloodTide(Double.valueOf(st_tideway_r.getV02h()));
                                    }else if(Double.parseDouble(TIDEList.getValue().get(i).getDir02h())<180&&
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir02h())>20){  //落潮
                                        //获取落潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir02h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfEbbTide(  Double.valueOf(st_tideway_r.getV02h()));

                                    }
                                }
                                sensors.setSensorType(SensorType.V02H);
                                break;
                            case  "DIR02H":
                                if (TIDEList.getValue().get(i).getDir02h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getDir02h())));
                                }
                                sensors.setSensorType(SensorType.DIR02H);
                                break;
                            case  "V04H":
                                if (TIDEList.getValue().get(i).getV04h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getV04h())));


                                    //流速
                                    if (Double.parseDouble(TIDEList.getValue().get(i).getDir04h())>180||
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir04h())<20) {//涨潮
                                        //获取涨潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir04h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfFloodTide(Double.valueOf(st_tideway_r.getV04h()));
                                    }else if(Double.parseDouble(TIDEList.getValue().get(i).getDir04h())<180&&
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir04h())>20){  //落潮
                                        //获取落潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir04h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfEbbTide(Double.valueOf(st_tideway_r.getV04h()));
                                    }

                                }
                                sensors.setSensorType(SensorType.V04H);
                                break;
                            case  "DIR04H":
                                if (TIDEList.getValue().get(i).getDir04h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getDir04h())));
                                }
                                sensors.setSensorType(SensorType.DIR04H);
                                break;
                            case  "V06H":
                                if (TIDEList.getValue().get(i).getV06h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getV06h())));
                                    //流速
                                    if (Double.parseDouble(TIDEList.getValue().get(i).getDir06h())>180||
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir06h())<20) {//涨潮
                                        //获取涨潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir06h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfFloodTide(Double.valueOf(st_tideway_r.getV06h()));
                                    }else   if (Double.parseDouble(TIDEList.getValue().get(i).getDir06h())<180&&
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir06h())>20)  {  //落潮
                                        //获取落潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir06h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfEbbTide(Double.valueOf(st_tideway_r.getV06h()));
                                    }
                                }
                                sensors.setSensorType(SensorType.V06H);
                                break;
                            case  "DIR06H":
                                if (TIDEList.getValue().get(i).getDir06h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getDir06h())));
                                }
                                sensors.setSensorType(SensorType.DIR06H);
                                break;
                            case  "V08H":
                                if (TIDEList.getValue().get(i).getV08h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getV08h())));
                                    //流速
                                    if (Double.parseDouble(TIDEList.getValue().get(i).getDir08h())>180||
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir08h())<20) {//涨潮
                                        //获取涨潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir08h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfFloodTide(Double.valueOf(st_tideway_r.getV08h()));
                                    }else if (Double.parseDouble(TIDEList.getValue().get(i).getDir08h())<180&&
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir08h())>20) {  //落潮
                                        //获取落潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir08h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfEbbTide(Double.valueOf(st_tideway_r.getV08h()));

                                    }
                                }
                                sensors.setSensorType(SensorType.V08H);
                                break;
                            case  "DIR08H":
                                if (TIDEList.getValue().get(i).getDir08h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getDir08h())));
                                }
                                sensors.setSensorType(SensorType.DIR08H);
                                break;
                            case  "V10H":
                                if (TIDEList.getValue().get(i).getV10h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getV10h())));
                                    //流速
                                    if (Double.parseDouble(TIDEList.getValue().get(i).getDir10h())>180||
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir10h())<20) {//涨潮
                                        //获取涨潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir10h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfFloodTide(Double.valueOf(st_tideway_r.getV10h()));
                                    }else  if (Double.parseDouble(TIDEList.getValue().get(i).getDir10h())<180&&
                                            Double.parseDouble(TIDEList.getValue().get(i).getDir10h())>20){  //落潮
                                        //获取落潮的最大流向
                                        ST_TIDEWAY_R st_tideway_r = TIDEList.getValue().stream().max(Comparator.comparing(ST_TIDEWAY_R::getDir10h)).orElse(null);
                                        //涨潮最大流速
                                        sensors.setMaximumVelocityOfEbbTide(Double.valueOf(st_tideway_r.getV10h()));
                                    }
                                }
                                sensors.setSensorType(SensorType.V10H);
                                break;
                            case  "DIR10H":
                                if (TIDEList.getValue().get(i).getDir10h() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getDir10h())));
                                }
                                sensors.setSensorType(SensorType.DIR10H);
                                break;
                            case  "AVV":
                                if (TIDEList.getValue().get(i).getAvv() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAvv())));
                                }
                                sensors.setSensorType(SensorType.AVV);
                                break;
                            case  "AVDIR":
                                if (TIDEList.getValue().get(i).getAvdir() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAvdir())));
                                }
                                sensors.setSensorType(SensorType.AVDIR);
                                break;
                        }
                    }
                    if (sensors.getDataList() != null) {
                        sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                        sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                        sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                        sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                        sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                    }
                    sensorsList.add(sensors);
                }
                break;


            //风速表
            case "WNDV":
            case "WNDDIR":
            case "AD":
            case "AC":
            case "AF":
            case "AE":
            case "AFMX":
            case "AEMX":
            case "ABAVWVTM":
            case "ABAVWV":
            case "ABMX":
            case "AAMX":
                List<ST_WIND_R> ST_WIND_RList = st_wind_rService.selectByStationArrys(dataCodes, startTM, endTM);
                Map<String, List<ST_WIND_R>> ST_Wind_RListGROUP = ST_WIND_RList.stream()
                        .collect(Collectors.groupingBy(ST_WIND_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_WIND_R>> TIDEList : ST_Wind_RListGROUP.entrySet()) {
                    Sensors sensors = new Sensors();
                    sensors.setDatacode(TIDEList.getKey());
                    for (int i = 0; i < TIDEList.getValue().size(); i++) {
                        switch (sensorType) {
                            case  "WNDV":
                                if (TIDEList.getValue().get(i).getWndv() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(),TIDEList.getValue().get(i).getWndv()));
                                }
                                sensors.setSensorType(SensorType.WNDV);
                                break;
                            case  "WNDDIR":
                                if (TIDEList.getValue().get(i).getWnddir() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getWnddir())));
                                }
                                sensors.setSensorType(SensorType.WNDDIR);
                                break;
                            case  "AD":
                                if (TIDEList.getValue().get(i).getAd() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), TIDEList.getValue().get(i).getAd()));
                                }
                                sensors.setSensorType(SensorType.AD);
                                break;
                            case  "AC":
                                if (TIDEList.getValue().get(i).getAc() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAc())));
                                }
                                sensors.setSensorType(SensorType.AC);
                                break;
                            case  "AF":
                                if (TIDEList.getValue().get(i).getAf() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAf())));
                                }
                                sensors.setSensorType(SensorType.AF);
                                break;
                            case  "AE":
                                if (TIDEList.getValue().get(i).getAe() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAe())));
                                }
                                sensors.setSensorType(SensorType.AE);
                                break;
                            case  "AFMX":
                                if (TIDEList.getValue().get(i).getAfmx() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAfmx())));
                                }
                                sensors.setSensorType(SensorType.AFMX);
                                break;
                            case  "AEMX":
                                if (TIDEList.getValue().get(i).getAemx() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAemx())));
                                }
                                sensors.setSensorType(SensorType.AEMX);
                                break;
                            case  "ABAVWVTM":
                                if (TIDEList.getValue().get(i).getAbavwvtm() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAbavwvtm())));
                                }
                                sensors.setSensorType(SensorType.ABAVWVTM);
                                break;
                            case  "ABAVWV":
                                if (TIDEList.getValue().get(i).getAbavwv() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAbavwv())));
                                }
                                sensors.setSensorType(SensorType.ABAVWV);
                                break;
                            case  "ABMX":
                                if (TIDEList.getValue().get(i).getAbmx() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAbmx())));
                                }
                                sensors.setSensorType(SensorType.ABMX);
                                break;
                            case  "AAMX":
                                if (TIDEList.getValue().get(i).getAamx() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getTm(), Double.valueOf(TIDEList.getValue().get(i).getAamx())));
                                }
                                sensors.setSensorType(SensorType.AAMX);
                                break;
                        }
                    }
                    if (sensors.getDataList() != null) {
                        sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                        sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                        sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                        sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                        sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                    }
                    sensorsList.add(sensors);
                }
                break;
            //水质wq
            case "TN":
            case "TP":
            case "NH4":
            case "PH":
            case "DOX":
            case "TURB":
            case "CodMn":
            case "WTMP":
                List<WQ_AWQMD_D> WQ_AWQMD_DList = wq_awqmd_dService.queryAllByStcds(dataCodes, startTM, endTM);
                Map<String, List<WQ_AWQMD_D>> WQ_AWQMD_DListGROUP = WQ_AWQMD_DList.stream()
                        .collect(Collectors.groupingBy(WQ_AWQMD_D::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<WQ_AWQMD_D>> TIDEList : WQ_AWQMD_DListGROUP.entrySet()) {
                    Sensors sensors = new Sensors();
                    sensors.setDatacode(TIDEList.getKey());
                    for (int i = 0; i < TIDEList.getValue().size(); i++) {
                        switch (sensorType) {
                            case  "TN":
                                if (TIDEList.getValue().get(i).getTn() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getSpt(),TIDEList.getValue().get(i).getTn()));
                                }
                                sensors.setSensorType(SensorType.TN);
                                break;
                            case  "TP":
                                if (TIDEList.getValue().get(i).getTp() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getTp())));
                                }
                                sensors.setSensorType(SensorType.TP);
                                break;
                            case  "NH4":
                                if (TIDEList.getValue().get(i).getNh3n() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getSpt(), TIDEList.getValue().get(i).getNh3n()));
                                }
                                sensors.setSensorType(SensorType.NH4);
                                break;
                            case  "DOX":
                                if (TIDEList.getValue().get(i).getDox() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getDox())));
                                }
                                sensors.setSensorType(SensorType.DOX);
                                break;
                            case  "TURB":
                                if (TIDEList.getValue().get(i).getTurb() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getTurb())));
                                }
                                sensors.setSensorType(SensorType.TURB);
                                break;
                            case  "CodMn":
                                if (TIDEList.getValue().get(i).getCodmn() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getCodmn())));
                                }
                                sensors.setSensorType(SensorType.CodMn);
                                break;
                            case  "PH":
                                if (TIDEList.getValue().get(i).getPh() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getPh())));
                                }
                                sensors.setSensorType(SensorType.PH);
                                break;
                            case  "WTMP":
                                if (TIDEList.getValue().get(i).getWtmp() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getWtmp())));
                                }
                                sensors.setSensorType(SensorType.WTMP);
                            case  "COND":
                                if (TIDEList.getValue().get(i).getWtmp() != null) {
                                    sensors.setDataList(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getCond())));
                                }
                                sensors.setSensorType(SensorType.COND);
                                break;

                        }
                    }
                    if (sensors.getDataList() != null) {
                        sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                        sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                        sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                        sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                        sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                    }
                    sensorsList.add(sensors);
                }
                break;
            default:
                break;
        }
        return sensorsList;


    }

    //查询实况雨量数据
    public List<Sensors> getpptndata(List<String> dataCodes, String startTM, String endTM,
                                     StepLength staticalDataType) {
        List<Sensors> sensorsList = new ArrayList<>();
        List<ST_PPTN_R> ST_PPTN_RLists = new ArrayList<>();
        switch (staticalDataType) {
            case FiveMinute:
                ST_PPTN_RLists = ST_PPTN_R5Service.selectByStationArrys(dataCodes, startTM, endTM);
                break;
            case Hour:
            case Day:
                ST_PPTN_RLists = ST_PPTN_RService.selectByStationArrys(dataCodes, startTM, endTM);
                break;
            default:
                break;
        }
        if (staticalDataType == StepLength.Day) { // 日雨量
            Map<String, List<ST_PPTN_R>> PPTNListGroup = ST_PPTN_RLists.stream().collect(Collectors.groupingBy(ST_PPTN_R::getStcd, Collectors.toList()));
            for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
                Sensors sensors = new Sensors();
                sensors.setDatacode(PPTNList.getKey());
                for (int i = 0; i < PPTNList.getValue().size(); i++) {
                    if (PPTNList.getValue().get(i).getDyp() != null) {
                        sensors.setDataList(new Data(PPTNList.getValue().get(i).getTm(), PPTNList.getValue().get(i).getDyp()));
                        sensors.setSensorType(SensorType.P);
                    }

                }
                if (sensors.getDataList() != null) {
                    sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                    sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                    sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                    sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                    sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                    sensors.setTotalValue(sensors.getDataList().stream().mapToDouble(Data::getValue).sum());
                    sensorsList.add(sensors);
                }
            }
        } else {
            Map<String, List<ST_PPTN_R>> PPTNListGroup = ST_PPTN_RLists.stream().collect(Collectors.groupingBy(ST_PPTN_R::getStcd, Collectors.toList()));
            for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
                Sensors sensors = new Sensors();
                sensors.setDatacode(PPTNList.getKey());
                for (int i = 0; i < PPTNList.getValue().size(); i++) {
                    if (PPTNList.getValue().get(i).getDrp() != null) {
                        sensors.setDataList(new Data(PPTNList.getValue().get(i).getTm(), PPTNList.getValue().get(i).getDrp()));
                        sensors.setSensorType(SensorType.P);

                    }
                }
                if (sensors.getDataList() != null) {
                    sensors.setStartTM(sensors.getDataList().stream().min(Comparator.comparing(Data::getTM)).get().getTM());
                    sensors.setEndTM(sensors.getDataList().stream().max(Comparator.comparing(Data::getTM)).get().getTM());
                    sensors.setMinData(sensors.getDataList().stream().min(Comparator.comparing(Data::getValue)).get());
                    sensors.setAvgValue(sensors.getDataList().stream().mapToDouble(Data::getValue).average().getAsDouble());
                    sensors.setTotalValue(sensors.getDataList().stream().mapToDouble(Data::getValue).sum());
                    sensors.setMaxData(sensors.getDataList().stream().max(Comparator.comparing(Data::getValue)).get());
                    sensorsList.add(sensors);
                }
            }
        }

        return sensorsList;
    }

    // 查询最新数据
    public List<Sensors_NewsData> getNewesData(String sensorType, List<String> dataCodes,int PID,String dataTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 日期格式转换
        Calendar calendar = new GregorianCalendar();
        String StartTM =null;
        String EndTM;
        List<Sensors_NewsData> sensors_NewsDataList = new ArrayList<>();
        switch (sensorType) {
            case "P":
                List<ST_PPTN_R> ST_PPTN_RLists = new ArrayList<>();
                //先查询五分钟最新数据 不为空则赋值五分钟数据表数据
                ST_PPTN_RLists = ST_PPTN_R5Service.selectPPTNNewEstData(dataCodes);
                if (ST_PPTN_RLists == null) {//为空则赋值小时数据表数据
                    ST_PPTN_RLists = ST_PPTN_RService.selectPPTNNewEstData(dataCodes);
                }
                //雨量特征
              //  List<ST_PPTN_R> sT_PPTN_RDRP3 = null;
               // BDMS_ST_SENSORALARM_B bdms_st_sensoralarm_b = null;
                // Date start = null;
                int num=0;
                String steplen = null;
                HashMap<String,RainStatictisData> rainStatictisDataList3hour = new HashMap<>();
                HashMap<String,RainStatictisData> rainStatictisDataList6hour = new HashMap<>();;
                HashMap<String,RainStatictisData> rainStatictisDataList12hour = new HashMap<>();;
                HashMap<String,RainStatictisData> rainStatictisDataListYesterday = new HashMap<>();;
                HashMap<String,RainStatictisData> rainStatictisDataListToday = new HashMap<>();;
                //
                List<BDMS_ST_SENSORALARM_B> sensoralarms = BDMS_ST_SENSORALARM_BService.selectByStcd(dataCodes,"P", PID);
                //特征统计值
                for (int l=0;l<5;l++) {
                    Date start = new Date();
                    start.setMinutes(0);
                    start.setSeconds(0);
                    String endTM=null;
                    if (dataTime != null&& !dataTime.isEmpty()) {
                        StartTM = dataTime;
                    }else {
                        StartTM = format.format(start);
                    }
                    switch (l) {
                        case 0:
                            endTM =format.format(start);
                            steplen="近三小时";
                            num = 3;
                            start.setHours(start.getHours() - num);
                            StartTM = format.format(start);
                            rainStatictisDataList3hour=GetRainStatistic(dataCodes,"P",sensoralarms,steplen,StartTM,endTM);
                            break;
                        case 1:
                            endTM =format.format(start);
                            num = 6;
                            steplen="近六小时";
                            start.setHours(start.getHours() - num);
                            StartTM = format.format(start);
                            rainStatictisDataList6hour=GetRainStatistic(dataCodes,"P",sensoralarms,steplen,StartTM,endTM);
                            break;
                        case 2:
                            endTM =format.format(start);
                            num = 12;
                            steplen="近十二小时";
                            start.setHours(start.getHours() - num);
                            StartTM = format.format(start);
                            rainStatictisDataList12hour=GetRainStatistic(dataCodes,"P",sensoralarms,steplen,StartTM,endTM);
                            break;
                        case 3:
//
                            if (start.getHours()<= 8){
                                num = 48;
                                start.setHours(8);
                                calendar.setTime(start);
                                calendar.add(Calendar.DATE ,-1);
                            }else {
                                start.setHours(8);
                                calendar.setTime(start);
                                num = 24;
                            }

                            start.setHours(8);
                            endTM =format.format(calendar.getTime());
                            steplen="昨日雨量";
                            start.setHours(start.getHours() - num);
                            StartTM = format.format(start);
                            rainStatictisDataListYesterday=GetRainStatistic(dataCodes,"P",sensoralarms,steplen,StartTM,endTM);

                            break;
                        case 4:

                            if (start.getHours()<= 8){
                                num = 24;
                                start.setHours(8);
                                endTM =format.format(start);
                            }else {
                                num = 0;
                                start.setHours(8);
                                calendar.setTime(start);
                                calendar.add(Calendar.DATE ,+1);

                                endTM =format.format(calendar.getTime());
                            }
                            steplen="今日雨量";
                            start.setHours(start.getHours() - num);
                            StartTM = format.format(start);
                            rainStatictisDataListToday=GetRainStatistic(dataCodes,"P",sensoralarms,steplen,StartTM,endTM);
                            break;
                        default:
                            break;
                    }
                }

                //最新
                for (int i=0;i<dataCodes.size();i++){
                    String dataCode =dataCodes.get(i);
                    Sensors_NewsData sensors_NewsData = new Sensors_NewsData();
                    sensors_NewsData.setDatacode(dataCodes.get(i));
                    sensors_NewsData.setSensorType(SensorType.P);
                    Data data  =new Data();
                    ST_PPTN_R st_pptn_r= ST_PPTN_RLists.stream().filter(ST_PPTN_R -> ST_PPTN_R.getStcd().equals(dataCode)).findAny().orElse(null);
                    if (st_pptn_r!=null){
                        data.setValue(st_pptn_r.getDrp());
                        data.setTM(st_pptn_r.getTm());
                        sensors_NewsData.setNewesdata(data);
                        for (BDMS_ST_SENSORALARM_B sensoralarm : sensoralarms) {
                            if (sensoralarm.getAlarmvalue()!=null){
                                sensors_NewsData.setDistanceAlert(String.valueOf(sensoralarm.getAlarmvalue()));
                                //实测 减去 警戒
                                double  alarmValue =st_pptn_r.getDrp()  -sensoralarm.getAlarmvalue()  ;

                                if (alarmValue < -0.5) {
                                    //正常
                                    sensors_NewsData.setAlarmlevel("正常");
                                    sensors_NewsData.setAlarmColor("#000000");
                                } else if (alarmValue >= -0.5 && alarmValue <= 0) {
                                    //即将超警
                                    sensors_NewsData.setAlarmlevel("即将超警");
                                    sensors_NewsData.setAlarmColor("#FFBC00");
                                }
                                else {
                                    //超警
                                    sensors_NewsData.setAlarmlevel("超警");
                                    sensors_NewsData.setAlarmColor("#FF0000");
                                }
                            }


                        }
                    }
                    //
                    for (String key:rainStatictisDataList3hour.keySet()) {
                        if(key.equals(dataCode)) {
                            sensors_NewsData.setRainStatictisData(rainStatictisDataList3hour.get(key));
                            break;
                        }
                    }
                    for (String key:rainStatictisDataList6hour.keySet()) {
                        if(key.equals(dataCode)) {
                            sensors_NewsData.setRainStatictisData(rainStatictisDataList6hour.get(key));
                            break;
                        }
                    }
                    for (String key:rainStatictisDataList12hour.keySet()) {
                        if(key.equals(dataCode)) {
                            sensors_NewsData.setRainStatictisData(rainStatictisDataList12hour.get(key));
                            break;
                        }
                    }
                    for (String key:rainStatictisDataListYesterday.keySet()) {
                        if(key.equals(dataCode)) {
                            sensors_NewsData.setRainStatictisData(rainStatictisDataListYesterday.get(key));
                            break;
                        }
                    }
                    for (String key:rainStatictisDataListToday.keySet()) {
                        if(key.equals(dataCode)) {
                            sensors_NewsData.setRainStatictisData(rainStatictisDataListToday.get(key));
                            break;
                        }
                    }
                    sensors_NewsDataList.add(sensors_NewsData);
                }
                break;
            case "Z":
            case "Q":
                List<ST_RIVER_R> ST_RIVER_RList = new ArrayList<ST_RIVER_R>();
//                ST_RIVER_RList = ST_RIVER_R5Service.selectRIVERNewEstData(dataCodes);
//                if (ST_RIVER_RList == null) {
                    ST_RIVER_RList = ST_RIVER_RService.selectRIVERNewEstData(dataCodes);
              //  }
                List<BDMS_ST_SENSORALARM_B> sensoralarm = BDMS_ST_SENSORALARM_BService.selectByStcd(dataCodes,"Z", PID);
                boolean res1 = ST_RIVER_RList.size()>0;
                if (res1) {
                    for (int i=0;i<dataCodes.size();i++){
                        String dataCode =dataCodes.get(i);
                        Sensors_NewsData sensors_NewsData = new Sensors_NewsData();
                        sensors_NewsData.setDatacode(dataCodes.get(i));
                        Data data = new Data();
                        ST_RIVER_R st_river_r= ST_RIVER_RList.stream().filter(ST_RIVER_R -> ST_RIVER_R.getStcd().equals(dataCode)).findAny().orElse(null);

                        if ("Z".equals(sensorType)) {// 流量
                            sensors_NewsData.setSensorType(SensorType.Z);
                            if (st_river_r!=null){
                                data.setValue(st_river_r.getZ());
                                data.setTM(st_river_r.getTm());
                                sensors_NewsData.setNewesdata(data);
                                for (BDMS_ST_SENSORALARM_B sensoralarm_b : sensoralarm) {
                                    if (sensoralarm_b.getAlarmlevel()!=null) {
                                        //实测 减去 警戒
                                        double alarmValue = data.getValue() - sensoralarm_b.getAlarmvalue();

                                        if (alarmValue < -0.5) {
                                            //正常
                                            sensors_NewsData.setAlarmlevel("正常");
                                            sensors_NewsData.setAlarmColor("#000000");
                                        } else if (alarmValue >= -0.5 && alarmValue <= 0) {
                                            //即将超警
                                            sensors_NewsData.setAlarmlevel("即将超警");
                                            sensors_NewsData.setAlarmColor("#FFBC00");
                                        } else {
                                            //超警
                                            sensors_NewsData.setAlarmlevel("超警");
                                            sensors_NewsData.setAlarmColor("#FF0000");
                                        }

                                    }

                                }
                            }

                        }else {
                            sensors_NewsData.setSensorType(SensorType.Q);
                            if (st_river_r!=null){
                                data.setValue(st_river_r.getQ());
                                data.setTM(st_river_r.getTm());
                                sensors_NewsData.setNewesdata(data);
                            }
                        }
                        sensors_NewsDataList.add(sensors_NewsData);
                        }
                    }
                break;
            case "INQ":
            case "OTQ":
            case "RZ":
            case "W":
                List<ST_RSVR_R> ST_RSVR_RList = new ArrayList<ST_RSVR_R>();
//                ST_RSVR_RList = ST_RSVR_R5Service.selectRSVRNewEstData(dataCodes);
//                if (ST_RSVR_RList == null) {
                    ST_RSVR_RList = ST_RSVR_RService.selectRSVRNewEstData(dataCodes);
              // }
                boolean res2 = ST_RSVR_RList.size()>0;
                if (res2) {
                    for (int i=0;i<dataCodes.size();i++){
                        String dataCode =dataCodes.get(i);
                        Sensors_NewsData sensors_NewsData = new Sensors_NewsData();
                        sensors_NewsData.setDatacode(dataCodes.get(i));
                        Data data = new Data();
                        ST_RSVR_R st_rsvr_r= ST_RSVR_RList.stream().filter(ST_RSVR_R -> ST_RSVR_R.getStcd().equals(dataCode)).findAny().orElse(null);

                        switch (sensorType) {
                            case "RZ":
                                List<BDMS_ST_SENSORALARM_B> sensoralarmRZ = BDMS_ST_SENSORALARM_BService.selectByStcd(dataCodes,"RZ", PID);
                                sensors_NewsData.setSensorType(SensorType.RZ);
                                if (st_rsvr_r!=null){
                                    data.setValue(st_rsvr_r.getRz());
                                    data.setTM(st_rsvr_r.getTm());
                                    sensors_NewsData.setNewesdata(data);
                                    for (BDMS_ST_SENSORALARM_B sensoralarm_b : sensoralarmRZ) {
                                        if (sensoralarm_b.getAlarmlevel()!=null) {
                                                //实测 减去 警戒
                                                double alarmValue = data.getValue() - sensoralarm_b.getAlarmvalue();
                                                if (alarmValue < -0.5) {
                                                    //正常
                                                    sensors_NewsData.setAlarmlevel("正常");
                                                    sensors_NewsData.setAlarmColor("#000000");
                                                } else if (alarmValue >= -0.5 && alarmValue <= 0) {
                                                    //即将超警
                                                    sensors_NewsData.setAlarmlevel("即将超警");
                                                    sensors_NewsData.setAlarmColor("#FFBC00");
                                                } else {
                                                    //超警
                                                    sensors_NewsData.setAlarmlevel("超警");
                                                    sensors_NewsData.setAlarmColor("#FF0000");
                                                }
                                            }
                                        }
                                    }
                                break;
                            case "INQ":
                                sensors_NewsData.setSensorType(SensorType.INQ);
                                if (st_rsvr_r!=null) {
                                    data.setValue(st_rsvr_r.getInq());
                                    data.setTM(st_rsvr_r.getTm());
                                    sensors_NewsData.setNewesdata(data);
                                }
                                break;
                            case "OTQ":
                                sensors_NewsData.setSensorType(SensorType.OTQ);
                                if (st_rsvr_r!=null) {
                                    data.setValue(st_rsvr_r.getOtq());
                                    data.setTM(st_rsvr_r.getTm());
                                    sensors_NewsData.setNewesdata(data);
                                }
                                break;
                            case "W":
                                sensors_NewsData.setSensorType(SensorType.W);
                                if (st_rsvr_r!=null) {
                                    data.setValue(st_rsvr_r.getW());
                                    data.setTM(st_rsvr_r.getTm());
                                    sensors_NewsData.setNewesdata(data);
                                }
                                break;

                        }

                        sensors_NewsDataList.add(sensors_NewsData);
                    }
                }
                break;

            case "TDZ":
                List<ST_TIDE_R> ST_TIDE_RList = new ArrayList<ST_TIDE_R>();
                List<String> stcds = new ArrayList<>();
                //获取所有站点的警戒潮位值
                List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bdms_st_sensor_bService.selectByDataCodes(PID,dataCodes);
                for (BDMS_ST_SENSOR_B bdms_st_sensor_b:bdms_st_sensor_bs){
                    stcds.add(bdms_st_sensor_b.getStcd());
                }

                List<BDMS_ST_SENSORALARM_B> bdms_st_sensoralarm_bs = BDMS_ST_SENSORALARM_BService.selectByStcd(stcds,"TDZ",PID);

                for (int i = 0; i < dataCodes.size(); i++) {
                    Sensors_NewsData sensors_NewsData = new Sensors_NewsData();
                    String dataCode = dataCodes.get(i);
                    System.out.println(datasourceType);
                    ST_TIDE_R ST_TIDE_R = ST_TIDE_RService.selectTIDENewEstData(dataCode,datasourceType);



                    sensors_NewsData.setDatacode(dataCode);

                    BDMS_ST_SENSOR_B b=  bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B->BDMS_ST_SENSOR_B.getDatacode().equals(dataCode)).findAny().orElse(null);
                    //System.out.println(b);
                    String stcd = b.getStcd();
                    BDMS_ST_SENSORALARM_B b1=  bdms_st_sensoralarm_bs.stream().filter(BDMS_ST_SENSORALARM_B->BDMS_ST_SENSORALARM_B.getStcd().equals(stcd)).findAny().orElse(null);

                    // System.out.println(b1);
                    if (ST_TIDE_R!=null){
                        ST_TIDE_RList.add(ST_TIDE_R);
                        sensors_NewsData.setNewesdata(new Data(ST_TIDE_R.getTm(), ST_TIDE_R.getTdz()));
                       if (b1!=null){
                           sensors_NewsData.setDistanceAlert(String.format("%.2f",b1.getAlarmvalue()-ST_TIDE_R.getTdz()));
                       }

                    }
                    sensors_NewsData.setSensorType(SensorType.TDZ);
                    sensors_NewsDataList.add(sensors_NewsData);
                }

                break;
            case "UPZ":
            case "DWZ":
            case "TGTQ":
                List<ST_WAS_R> ST_WAS_RList = new ArrayList<ST_WAS_R>();
                for (String dataCode : dataCodes) {
                    ST_WAS_R sT_WAS_R = null;
                    sT_WAS_R = ST_WAS_RService.selectWASNewEstData(dataCode);
                    if (sT_WAS_R != null) {
                        ST_WAS_RList.add(sT_WAS_R);
                    }
                }
                Map<String, List<ST_WAS_R>> WASListGroup = ST_WAS_RList.stream()
                        .collect(Collectors.groupingBy(ST_WAS_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_WAS_R>> WASList : WASListGroup.entrySet()) {
                    Sensors_NewsData sensors_NewsData = new Sensors_NewsData();
                    sensors_NewsData.setDatacode(WASList.getKey());

                    for (int i = 0; i < WASList.getValue().size(); i++) {
                        switch (sensorType) {
                            case "UPZ":
                                if (WASList.getValue().get(i).getUpz() != null) {//闸上水位
                                    sensors_NewsData.setNewesdata(new Data(WASList.getValue().get(i).getTm(), WASList.getValue().get(i).getUpz()));
                                    sensors_NewsData.setSensorType(SensorType.UPZ);
                                }
                                break;
                            case "DWZ":
                                if (WASList.getValue().get(i).getDwz() != null) {//闸下水位
                                    sensors_NewsData.setNewesdata(new Data(WASList.getValue().get(i).getTm(), WASList.getValue().get(i).getDwz()));
                                    sensors_NewsData.setSensorType(SensorType.DWZ);
                                }
                                break;
                            case "TGTQ":
                                if (WASList.getValue().get(i).getTgtq() != null) {//总过闸流量
                                    sensors_NewsData.setNewesdata(new Data(WASList.getValue().get(i).getTm(), WASList.getValue().get(i).getTgtq()));
                                    sensors_NewsData.setSensorType(SensorType.TGTQ);
                                }
                                break;
                        }
                    }

                    sensors_NewsDataList.add(sensors_NewsData);
                }


                break;
            case "PPUPZ":
            case "PPDWZ":
            case "PMPQ":
                List<ST_PUMP_R> ST_PUMP_RList = new ArrayList<ST_PUMP_R>();
                for (String dataCode : dataCodes) {
                    ST_PUMP_R sT_PUMP_R = null;

                    sT_PUMP_R = ST_PUMP_RService.selectPUMPNewEstData(dataCode);
                    ST_PUMP_RList.add(sT_PUMP_R);
                }
                System.out.println(ST_PUMP_RList);
                Map<String, List<ST_PUMP_R>> PUMPListGroup = ST_PUMP_RList.stream()
                        .collect(Collectors.groupingBy(ST_PUMP_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_PUMP_R>> PUMPList : PUMPListGroup.entrySet()) {
                    Sensors_NewsData sensors_NewsData = new Sensors_NewsData();
                    sensors_NewsData.setDatacode(PUMPList.getKey());
                    for (int i = 0; i < PUMPList.getValue().size(); i++) {
                        switch (sensorType) {
                            case "PPUPZ":
                                if (PUMPList.getValue().get(i).getPpupz() != null) {//闸上水位
                                    sensors_NewsData.setNewesdata(new Data(PUMPList.getValue().get(i).getTm(), PUMPList.getValue().get(i).getPpupz()));
                                    sensors_NewsData.setSensorType(SensorType.PPUPZ);
                                }
                                break;
                            case "PPDWZ":
                                if (PUMPList.getValue().get(i).getPpdwz() != null) {//闸下水位
                                    sensors_NewsData.setNewesdata(new Data(PUMPList.getValue().get(i).getTm(), PUMPList.getValue().get(i).getPpdwz()));
                                    sensors_NewsData.setSensorType(SensorType.PPDWZ);
                                }
                                break;

                            case "PMPQ":
                                if (PUMPList.getValue().get(i).getPmpq() != null) {//总过闸流量
                                    sensors_NewsData.setNewesdata(new Data(PUMPList.getValue().get(i).getTm(), PUMPList.getValue().get(i).getPmpq()));
                                    sensors_NewsData.setSensorType(SensorType.PMPQ);
                                }
                                break;
                        }
                    }
                    sensors_NewsDataList.add(sensors_NewsData);
                }

                break;
            case "TN":
            case "TP":
            case "NH4":
            case "PH":
            case "DOX":
            case "TURB":
            case "CodMn":
            case "WTMP":
                List<WQ_AWQMD_D> WQ_RList = new ArrayList<WQ_AWQMD_D>();
                for (String dataCode : dataCodes) {
                    WQ_AWQMD_D sT_PUMP_R = null;

                    sT_PUMP_R =wq_awqmd_dService.selectAWQMDNewEstData(dataCode);
                    System.out.println(sT_PUMP_R);
                    WQ_RList.add(sT_PUMP_R);
                }
                System.out.println(WQ_RList);
                Map<String, List<WQ_AWQMD_D>> WQ_AWQMD_DListGROUP = WQ_RList.stream()
                        .collect(Collectors.groupingBy(WQ_AWQMD_D::getStcd, Collectors.toList()));


                for (Map.Entry<String, List<WQ_AWQMD_D>> TIDEList : WQ_AWQMD_DListGROUP.entrySet()) {
                    Sensors_NewsData sensors_NewsData = new Sensors_NewsData();
                    sensors_NewsData.setDatacode(TIDEList.getKey());
                    for (int i = 0; i < TIDEList.getValue().size(); i++) {
                        switch (sensorType) {
                            case  "TN":
                                sensors_NewsData.setNewesdata(new Data(TIDEList.getValue().get(i).getSpt(), TIDEList.getValue().get(i).getTn()));
                                sensors_NewsData.setSensorType(SensorType.TN);
                                break;
                            case  "TP":
                                if (TIDEList.getValue().get(i).getTp() != null) {
                                    sensors_NewsData.setNewesdata(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getTp())));
                                }
                                sensors_NewsData.setSensorType(SensorType.TP);
                                break;
                            case  "NH4":
                                if (TIDEList.getValue().get(i).getNh3n() != null) {
                                    sensors_NewsData.setNewesdata(
                                            new Data(TIDEList.getValue().get(i).getSpt(), TIDEList.getValue().get(i).getNh3n()));
                                }
                                sensors_NewsData.setSensorType(SensorType.NH4);
                                break;
                            case  "DOX":
                                if (TIDEList.getValue().get(i).getDox() != null) {
                                    sensors_NewsData.setNewesdata(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getDox())));
                                }
                                sensors_NewsData.setSensorType(SensorType.DOX);
                                break;
                            case  "TURB":
                                if (TIDEList.getValue().get(i).getTurb() != null) {
                                    sensors_NewsData.setNewesdata(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getTurb())));
                                }
                                sensors_NewsData.setSensorType(SensorType.TURB);
                                break;
                            case  "CodMn":
                                if (TIDEList.getValue().get(i).getCodmn() != null) {
                                    sensors_NewsData.setNewesdata(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getCodmn())));
                                }
                                sensors_NewsData.setSensorType(SensorType.CodMn);
                                break;
                            case  "PH":
                                if (TIDEList.getValue().get(i).getPh() != null) {
                                    sensors_NewsData.setNewesdata(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getPh())));
                                }
                                sensors_NewsData.setSensorType(SensorType.PH);
                                break;
                            case  "WTMP":
                                if (TIDEList.getValue().get(i).getWtmp() != null) {
                                    sensors_NewsData.setNewesdata(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getWtmp())));
                                }
                                sensors_NewsData.setSensorType(SensorType.WTMP);
                            case  "COND":
                                if (TIDEList.getValue().get(i).getWtmp() != null) {
                                    sensors_NewsData.setNewesdata(
                                            new Data(TIDEList.getValue().get(i).getSpt(), Double.valueOf(TIDEList.getValue().get(i).getCond())));
                                }
                                sensors_NewsData.setSensorType(SensorType.COND);
                                break;

                        }
                    }
                    sensors_NewsDataList.add(sensors_NewsData);
                }
                break;
            default:
                break;
        }
        return sensors_NewsDataList;
    }

    public HashMap<String,RainStatictisData> GetRainStatistic(List<String>dataCodes,String sensortp,List<BDMS_ST_SENSORALARM_B> sensoralArms,String steplen,String StartTM,String EndTM) {

        HashMap<String,RainStatictisData> rainStatictisDatas=new HashMap<String,RainStatictisData>();
        List <ST_PPTN_R>  pptndatas = ST_PPTN_R5Service.selectSUM1(dataCodes,StartTM, EndTM);

        if (pptndatas == null || pptndatas.size() == 0){
            pptndatas = ST_PPTN_RService.selectSUM1(dataCodes, StartTM, EndTM);
        }
        System.out.println(pptndatas);
        for (String stcd : dataCodes) {
            RainStatictisData rainStatictisData = new RainStatictisData();
            rainStatictisData.setStepLength(steplen);
            rainStatictisData.setStartTm(StartTM);
            rainStatictisData.setEndTm(EndTM);
            ST_PPTN_R st_pptn_r = pptndatas.stream().filter(ST_PPTN_R -> ST_PPTN_R.getStcd().equals(stcd)).findAny().orElse(null);
            if (st_pptn_r != null) {
                List<BDMS_ST_SENSORALARM_B> sensoralarms = sensoralArms.stream().filter(it -> it.getStcd().equals(stcd) && it.getSendortp().equals(sensortp)).collect(Collectors.toList());
                if (sensoralarms.size() == 0) {
                    rainStatictisData.setValue(st_pptn_r.getDrp());
                } else {
                    for (BDMS_ST_SENSORALARM_B sensoralarm : sensoralarms) {

                        if (st_pptn_r.getDrp() != null) {
                            rainStatictisData.setValue(st_pptn_r.getDrp());
                            if (st_pptn_r.getDrp() >= sensoralarm.getAlarmvalue()) {
                                rainStatictisData.setAlarmlevel(sensoralarm.getAlarmlevel());
                                break;
                            }
                        }

                    }
                }


            }
            rainStatictisDatas.put(stcd, rainStatictisData);
        }
        return rainStatictisDatas;
    }

    // 获取水情预报成果信息
    public List<HydrologicForecastC> GetForecastC(List<String> stcds, String unitname, String plcd, String startTM,
                                                  String endTM) {


        List<HydrologicForecastC> list = new ArrayList<HydrologicForecastC>();
        List<ST_FORECASTC_F> ST_FORECASTC_FList = ST_FORECASTC_FService.selectByTime(stcds, unitname, plcd, startTM,
                endTM);
        Map<String, List<ST_FORECASTC_F>> StcdListGroup = ST_FORECASTC_FList.stream()
                .collect(Collectors.groupingBy(ST_FORECASTC_F::getStcd, Collectors.toList()));

        for (Map.Entry<String, List<ST_FORECASTC_F>> StcdList : StcdListGroup.entrySet()) {
            HydrologicForecastC HydrologicForecastC = new HydrologicForecastC();

            for (int i = 0; i < StcdList.getValue().size(); i++) {
                ForecastC ForecastC = new ForecastC();
                HydrologicForecastC.setStcd(StcdList.getValue().get(i).getStcd());
                HydrologicForecastC.setUnitname(StcdList.getValue().get(i).getUnitname());
                ForecastC.setIymdh(StcdList.getValue().get(i).getIymdh());
                HydrologicForecastC.setPlcd(StcdList.getValue().get(i).getPlcd());
                ForecastC.setFymdh(StcdList.getValue().get(i).getFymdh());
                ForecastC.setComments(StcdList.getValue().get(i).getComments());
                ForecastC.setWnstatus(StcdList.getValue().get(i).getWnstatus());
                HydrologicForecastC.setForecastc(ForecastC);

            }
            list.add(HydrologicForecastC);
        }


        return list;

    }

    // 获取水情预报成果数据
    public List<HydrologicForecastData> GetForecastData(List<String> STCDs, String unitname, String plcd,
                                                        String fymdh, String iymdh) {
        // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //
        // 日期格式转换
        List<HydrologicForecastData> list = new ArrayList<HydrologicForecastData>(); // 返回集合
        // 根据站码和日期获取数据
        List<ST_FORECAST_F> ST_FORECAST_FList = ST_FORECAST_FService.selectByStcd(STCDs, unitname, plcd, fymdh, iymdh);

        // 根据 站码分组
        Map<String, List<ST_FORECAST_F>> StcdListGroup = ST_FORECAST_FList.stream()
                .collect(Collectors.groupingBy(ST_FORECAST_F::getStcd, Collectors.toList()));
        // 遍历分组后集合
        for (Map.Entry<String, List<ST_FORECAST_F>> StcdList : StcdListGroup.entrySet()) {
            HydrologicForecastData HydrologicForecastData = new HydrologicForecastData(); // 返回集合单对象

            for (int j = 0; j < StcdList.getValue().size(); j++) {
                DataValues dataValues = new DataValues();
                HydrologicForecastData.setstcd(StcdList.getValue().get(j).getStcd()); // 站码
                HydrologicForecastData.setUnitname(StcdList.getValue().get(j).getUnitname());// 预报单位
                HydrologicForecastData.setPlcd(StcdList.getValue().get(j).getPlcd());// 方案代码
                HydrologicForecastData.setFymdh(StcdList.getValue().get(j).getFymdh());// 依据时间
                HydrologicForecastData.setIymdh(StcdList.getValue().get(j).getIymdh());// 发布时间
                dataValues.setYmdh(StcdList.getValue().get(j).getYmdh()); // 发生时间
                dataValues.setQ(StcdList.getValue().get(j).getQ());// 流量
                dataValues.setZ(StcdList.getValue().get(j).getZ());// 水位

                HydrologicForecastData.setdataValues(dataValues);
            }
            System.out.println(HydrologicForecastData.getdataValues().size() + "t");
            list.add(HydrologicForecastData);

        }
        return list;


    }

    // 查询雨量水库最新数据
    public List<PRData> getPRNewesData(String sensorType, List<String> dataCodes, String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 日期格式转换
        Calendar calendar = new GregorianCalendar();
        String StartTM = null;
        String EndTM = null;
        List<PRData> PRDataList = new ArrayList<PRData>();
        switch (sensorType) {
            case "P":
                List<ST_PPTN_R> ST_PPTN_RLists = new ArrayList<ST_PPTN_R>();
                for (int i = 0; i < dataCodes.size(); i++) {
                    String dataCode = dataCodes.get(i);
                    ST_PPTN_R ST_PPTN_R = null;
                    //先查询五分钟最新数据 不为空则赋值五分钟数据表数据
                    ST_PPTN_R = ST_PPTN_R5Service.selectByTmNewEstData(dataCode, date);
                    if (ST_PPTN_R == null) {//为空则赋值小时数据表数据
                        ST_PPTN_R = ST_PPTN_RService.selectByTmNewEstData(dataCode, date);
                    }
                    if (ST_PPTN_R != null) {
                        ST_PPTN_RLists.add(ST_PPTN_R);
                    }
                }

                Map<String, List<ST_PPTN_R>> PPTNListGroup = ST_PPTN_RLists.stream().collect(Collectors.groupingBy(ST_PPTN_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
                    PRData pRData = new PRData();
                    pRData.setDatacode(PPTNList.getKey());
                    pRData.setSensorType(SensorType.P);
                    pRData.setSensorName("雨量");
                    try {
                        //今日八点雨量 //获取日雨量      //查询日雨量
                        Date start = format.parse(date);
                        if (start.getHours() <= 8) {
                            calendar.setTime(start);
                            calendar.add(Calendar.DATE, -1);// 把日期往前推一天.正数往后推,负数往前推
                            start = calendar.getTime();
                        }
                        start.setHours(8);
                        start.setMinutes(0);
                        start.setSeconds(0);
                        StartTM = format.format(start);
                        //结束时间
                        Date end = format.parse(date);
                        EndTM = format.format(end);
                        //判断五分钟和整小时
                        ST_PPTN_R sT_PPTN_RDRP1 = null;
                        sT_PPTN_RDRP1 = ST_PPTN_R5Service.selectSUM(PPTNList.getKey()
                                , StartTM, EndTM);
                        if (sT_PPTN_RDRP1 == null) {
                            sT_PPTN_RDRP1 = ST_PPTN_RService.selectSUM(PPTNList.getKey()
                                    , StartTM, EndTM);
                        }
                        if (sT_PPTN_RDRP1 != null) {
                            pRData.setNewesdata(new Data(date, sT_PPTN_RDRP1.getDrp()));
                        }
                        //雨量统计数据
                        try {
                            ST_PPTN_R sT_PPTN_RDRP3 = null;
                            Date start1 = null;
                            //过去二十四小时雨量之和
                            //开始时间
                            start1 = format.parse(date);
                            if (start1.getHours() <= 8) {
                                calendar.setTime(start);
                                calendar.add(Calendar.DATE, -2);// 把日期往前推一天.正数往后推,负数往前推
                                start1 = calendar.getTime();
                            } else {
                                calendar.setTime(start);
                                calendar.add(Calendar.DATE, -1);// 把日期往前推一天.正数往后推,负数往前推
                                start1 = calendar.getTime();
                            }
                            start1.setHours(8);
                            start1.setMinutes(0);
                            start1.setSeconds(0);
                            StartTM = format.format(start1);
                            //结束时间
                            Date end1 = format.parse(date);
                            EndTM = format.format(end1);
                            //判断五分钟和整小时
                            sT_PPTN_RDRP3 = ST_PPTN_R5Service.selectSUM(PPTNList.getKey()
                                    , StartTM, EndTM);
                            if (sT_PPTN_RDRP3 == null) {
                                sT_PPTN_RDRP3 = ST_PPTN_RService.selectSUM(PPTNList.getKey()
                                        , StartTM, EndTM);
                            }
                            if (sT_PPTN_RDRP3 != null) {
                                pRData.setYesterdayRainDate(new Data(date, sT_PPTN_RDRP3.getDrp()));
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    PRDataList.add(pRData);
                }
                break;

            case "RZ":
            case "W":
                List<ST_RSVR_R> ST_RSVR_RList = new ArrayList<ST_RSVR_R>();


                for (int i = 0; i < dataCodes.size(); i++) {

                    String dataCode = dataCodes.get(i);
                    ST_RSVR_R sT_RSVR_R = null;
                    sT_RSVR_R = ST_RSVR_R5Service.selectByTmNewEstData(dataCode, date);
                    if (sT_RSVR_R == null) {
                        sT_RSVR_R = ST_RSVR_RService.selectByTmNewEstData(dataCode, date);
                    }

                    if (sT_RSVR_R != null) {
                        ST_RSVR_RList.add(sT_RSVR_R);
                    }
                }
                Map<String, List<ST_RSVR_R>> RSVRListGroup = ST_RSVR_RList.stream()
                        .collect(Collectors.groupingBy(ST_RSVR_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_RSVR_R>> RSVRList : RSVRListGroup.entrySet()) {
                    PRData pRData = new PRData();
                    pRData.setDatacode(RSVRList.getKey());
                    for (int i = 0; i < RSVRList.getValue().size(); i++) {
                        switch (sensorType) {
                            case "RZ":
                                if (RSVRList.getValue().get(i).getRz() != null) {// 库水位
                                    pRData.setNewesdata(new Data(RSVRList.getValue().get(i).getTm(), RSVRList.getValue().get(i).getRz()));
                                    pRData.setSensorType(SensorType.RZ);
                                    pRData.setSensorName("库水位");
                                    try {
                                        Date tm = format.parse(date);
                                        if (tm.getHours() != 8) {
                                            tm.setHours(8);
                                        }
                                        //获取今日八点数据
                                        date = format.format(tm);
                                        pRData.setEightLevel(new Data(date, ST_RSVR_R5Service.selectByTmNewEstData(RSVRList.getValue().get(i).getStcd(), date).getRz()));

                                        //获取今日最高水位
                                        Date start = format.parse(date);
                                        List<ST_RSVR_R> sT_RSVR_RList = null;
                                        if (start.getHours() <= 8) {//当小时下于8点时
                                            calendar.setTime(start);
                                            calendar.add(Calendar.DATE, -1);// 把日期往前推一天.正数往后推,负数往前推
                                            start = calendar.getTime();
                                            start.setHours(8);
                                            StartTM = format.format(start);
                                            Date end = format.parse(date);
                                            end.setHours(8);
                                            EndTM = format.format(end);
                                        } else if (start.getHours() > 8) {
                                            start = calendar.getTime();
                                            start.setHours(8);
                                            StartTM = format.format(start);
                                            Date end = format.parse(date);
                                            calendar.setTime(end);
                                            calendar.add(Calendar.DATE, +1);// 把日期往前推一天.正数往后推,负数往前推
                                            end.setHours(8);
                                            EndTM = format.format(end);
                                        }
                                        sT_RSVR_RList = ST_RSVR_R5Service.selectBytime(RSVRList.getValue().get(i).getStcd(), StartTM
                                                , EndTM);
                                        if (sT_RSVR_RList == null) {
                                            sT_RSVR_RList = ST_RSVR_RService.selectBytime(RSVRList.getValue().get(i).getStcd(), StartTM
                                                    , EndTM);
                                        }
                                        Double PeakLevel = sT_RSVR_RList.stream().max(Comparator.comparing(ST_RSVR_R::getRz)).get().getRz();
                                        pRData.setPeakLevel(new Data(date, PeakLevel));
                                    } catch (ParseException e) {
                                        e.printStackTrace();
                                    }
                                }
                                break;
                            case "W":
                                if (RSVRList.getValue().get(i).getW() != null) {// 蓄水量
                                    pRData.setNewesdata(new Data(RSVRList.getValue().get(i).getTm(), RSVRList.getValue().get(i).getW()));
                                    pRData.setSensorType(SensorType.W);
                                    pRData.setSensorName("蓄水量");
                                }
                                break;
                        }
                    }
                    PRDataList.add(pRData);
                }
                break;


            default:
                break;
        }


        return PRDataList;


    }


    // 查询最新数据
    public List<PRData> getpptnrsvrNewesData(String sensorType, List<String> dataCodes,String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 日期格式转换
        Calendar calendar = new GregorianCalendar();
        String StartTM =null;
        String EndTM = null;
        List<PRData> sensors_NewsDataList = new ArrayList<>();
        switch (sensorType) {
            case "P":
                List<ST_PPTN_R> ST_PPTN_RLists = new ArrayList<>();
                //先查询五分钟最新数据 不为空则赋值五分钟数据表数据
                ST_PPTN_RLists = ST_PPTN_R5Service.selectPPTNNewEstData(dataCodes);
                if (ST_PPTN_RLists == null) {//为空则赋值小时数据表数据
                    ST_PPTN_RLists = ST_PPTN_RService.selectPPTNNewEstData(dataCodes);
                }
               //
                for (int i=0;i<dataCodes.size();i++){
                    String dataCode =dataCodes.get(i);
                    PRData sensors_NewsData = new PRData();
                    sensors_NewsData.setDatacode(dataCodes.get(i));
                    sensors_NewsData.setSensorType(SensorType.P);
                    Data data  =new Data();
                    ST_PPTN_R st_pptn_r= ST_PPTN_RLists.stream().filter(ST_PPTN_R -> ST_PPTN_R.getStcd().equals(dataCode)).findAny().orElse(null);
                    if (st_pptn_r!=null){
                        data.setValue(st_pptn_r.getDrp());
                        data.setTM(st_pptn_r.getTm());
                        sensors_NewsData.setNewesdata(data);
                    }
                    //
                    sensors_NewsDataList.add(sensors_NewsData);
                }
                break;
            case "RZ":
            case "W":
                List<ST_RSVR_R> ST_RSVR_RList = new ArrayList<ST_RSVR_R>();
                for (int i = 0; i < dataCodes.size(); i++) {
                    String dataCode = dataCodes.get(i);
                    ST_RSVR_R sT_RSVR_R;
                    sT_RSVR_R = ST_RSVR_R5Service.selectByTmNewEstData(dataCode, date);
                    if (sT_RSVR_R == null) {
                        sT_RSVR_R = ST_RSVR_RService.selectByTmNewEstData(dataCode, date);
                    }

                    if (sT_RSVR_R != null) {
                        ST_RSVR_RList.add(sT_RSVR_R);
                    }
                }
                Map<String, List<ST_RSVR_R>> RSVRListGroup = ST_RSVR_RList.stream().collect(Collectors.groupingBy(ST_RSVR_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_RSVR_R>> RSVRList : RSVRListGroup.entrySet()) {
                    PRData pRData = new PRData();
                    pRData.setDatacode(RSVRList.getKey());
                    for (int i = 0; i < RSVRList.getValue().size(); i++) {
                        switch (sensorType) {
                            case "RZ":
                                if (RSVRList.getValue().get(i).getRz() != null) {// 库水位
                                    pRData.setNewesdata(new Data(RSVRList.getValue().get(i).getTm(), RSVRList.getValue().get(i).getRz()));
                                    pRData.setSensorType(SensorType.RZ);
                                    pRData.setSensorName("库水位");
                                    try {
                                        Date tm = format.parse(date);
                                        if (tm.getHours() != 8) {
                                            tm.setHours(8);
                                        }
                                        //获取今日八点数据
                                        date = format.format(tm);
                                        pRData.setEightLevel(new Data(date, ST_RSVR_R5Service.selectByTmNewEstData(RSVRList.getValue().get(i).getStcd(), date).getRz()));

                                        //获取今日最高水位
                                        Date start = format.parse(date);
                                        List<ST_RSVR_R> sT_RSVR_RList = null;
                                        if (start.getHours() <= 8) {//当小时下于8点时
                                            calendar.setTime(start);
                                            calendar.add(Calendar.DATE, -1);// 把日期往前推一天.正数往后推,负数往前推
                                            start = calendar.getTime();
                                            start.setHours(8);
                                            StartTM = format.format(start);
                                            Date end = format.parse(date);
                                            end.setHours(8);
                                            EndTM = format.format(end);
                                        } else if (start.getHours() > 8) {
                                            start = calendar.getTime();
                                            start.setHours(8);
                                            StartTM = format.format(start);
                                            Date end = format.parse(date);
                                            calendar.setTime(end);
                                            calendar.add(Calendar.DATE, +1);// 把日期往前推一天.正数往后推,负数往前推
                                            end.setHours(8);
                                            EndTM = format.format(end);
                                        }
                                        sT_RSVR_RList = ST_RSVR_R5Service.selectBytime(RSVRList.getValue().get(i).getStcd(), StartTM
                                                , EndTM);
                                        if (sT_RSVR_RList == null) {
                                            sT_RSVR_RList = ST_RSVR_RService.selectBytime(RSVRList.getValue().get(i).getStcd(), StartTM
                                                    , EndTM);
                                        }
                                        Double PeakLevel = sT_RSVR_RList.stream().max(Comparator.comparing(ST_RSVR_R::getRz)).get().getRz();
                                        pRData.setPeakLevel(new Data(date, PeakLevel));
                                    } catch (ParseException e) {
                                        e.printStackTrace();
                                    }
                                }
                                break;
                            case "W":
                                if (RSVRList.getValue().get(i).getW() != null) {// 蓄水量
                                    pRData.setNewesdata(new Data(RSVRList.getValue().get(i).getTm(), RSVRList.getValue().get(i).getW()));
                                    pRData.setSensorType(SensorType.W);
                                    pRData.setSensorName("蓄水量");
                                }
                                break;
                        }
                    }
                    sensors_NewsDataList.add(pRData);
                }
                break;
        }
        return sensors_NewsDataList;
    }

    //获取pptndaa
    public PPGetData getPPData(String [] STCDS, String startTM, String endTM,
                               StepLength staticalDataType) {
        List<String> Stcds = Arrays.stream(STCDS).collect(Collectors.toList());
        //数据格式
        DecimalFormat df = new DecimalFormat("#0.0");


        //最大返回对象
        PPGetData multiStationRainfalll = new PPGetData();
        //multiStationRainfalll.setPid(pid);
        multiStationRainfalll.setUnit("mm");
        List<String> stcds = new ArrayList<>();
        //调用返回站名站码方法
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bDMS_ST_SENSOR_BService.selectReportForms(Stcds);
        System.out.println(bdms_st_sensor_bs.size());
        for (int i = 0; i < bdms_st_sensor_bs.size(); i++) {

            Data data = new Data();
            stcds.add(bdms_st_sensor_bs.get(i).getStcd());
            data.setStnm(bdms_st_sensor_bs.get(i).getStnm());
            data.setStcd(bdms_st_sensor_bs.get(i).getStcd());
            multiStationRainfalll.setStationInfos(data);
        }
        Data data1 = new Data();
        data1.setStnm("流域平均雨量");
        data1.setStcd("00000000");
        multiStationRainfalll.setStationInfos(data1);

        List<ST_PPTN_R> List = new ArrayList<>();
        switch (staticalDataType) {
            case FiveMinute:
                List = ST_PPTN_R5Service.selectByStationArrys(stcds, startTM, endTM);
                break;
            case Hour:
            case Day:
                List = ST_PPTN_RService.selectByStationArrys(stcds, startTM, endTM);
                break;
            default:
                break;
        }
        Map<String, List<ST_PPTN_R>> PPTNListGroup = List.stream().collect(Collectors.groupingBy(ST_PPTN_R::getData1, Collectors.toList()));
        for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
            PPData multiStationRainfallData = new PPData();
            multiStationRainfallData.setTime( PPTNList.getKey());
            Data dataAvg = new Data();
            for (int i = 0; i < PPTNList.getValue().size(); i++) {
                Data data = new Data();
                data.setStcd(PPTNList.getValue().get(i).getStcd());
                if (PPTNList.getValue().get(i).getDrp()!=null){
                    data.setValue(PPTNList.getValue().get(i).getDrp());
                    data.setTM(PPTNList.getValue().get(i).getData());
                    multiStationRainfallData.setDatas(data);
                    multiStationRainfallData.setTime(PPTNList.getKey());
                    double sum = multiStationRainfallData.getDatas().stream().mapToDouble(Data::getValue).sum()+0.0;
                    Double avg = sum / bdms_st_sensor_bs.size();
                    dataAvg.setStcd("00000000");
                    dataAvg.setValue(Double.parseDouble(df.format(avg)));
                    multiStationRainfallData.setBasinRainfallAvg(dataAvg);
                }
            }
            multiStationRainfalll.setPPDataList(multiStationRainfallData);
        }
        if (multiStationRainfalll.getPPDataList()!=null){
            multiStationRainfalll.getPPDataList().sort(Comparator.comparing(PPData::getTime));
        }
        return multiStationRainfalll;
    }

    //业务报表
    //单站雨量日报表
    public HourlyRainfall getSingleStationDailyRainfall(String stcd, String TM) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 日期格式转换
        Calendar calendar = new GregorianCalendar();
        //数据格式
        Double dou =0.000;
        //最大返回对象
        HourlyRainfall hourlyRainfall = new HourlyRainfall();
        String start = TM+"-01 08:00:00";
        try {
            Date end =format.parse(start);
            calendar.setTime(end);
            calendar.add(Calendar.MONTH,1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String endtm =format.format(calendar.getTime());
        List<ST_PPTN_R> ST_PPTN_RLists = ST_PPTN_RService.selecStationRainfall(stcd, start,endtm);
        Map<String, List<ST_PPTN_R>> PPTNListGroup = ST_PPTN_RLists.stream().collect(Collectors.groupingBy(ST_PPTN_R::getData1, Collectors.toList()));
        for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
            hourlyRainfall.setUnit("mm");
            HourlyData hourlyData = new HourlyData();
            for (int i = 0; i < PPTNList.getValue().size(); i++) {
                hourlyRainfall.setStationCode(PPTNList.getValue().get(i).getStcd());
                Data data = new Data();

                data.setTM(PPTNList.getValue().get(i).getData());
                if ( PPTNList.getValue().get(i).getDrp()!=null){
                    Double  val= PPTNList.getValue().get(i).getDrp()+dou;
                    Double  cuk = Double.valueOf(String.format("%.1f", val));
                    data.setValue(cuk);
                    hourlyData.setDatas(data);
                }

                hourlyData.setHour(PPTNList.getKey());
            }
            hourlyRainfall.setHourlyDatas(hourlyData);
        }
        if(hourlyRainfall.getHourlyDatas()!=null){
            hourlyRainfall.getHourlyDatas().sort(Comparator.comparing(HourlyData::getHour));
        }
        return hourlyRainfall;
    }

    //单站雨量月报表
    public MonthRainfall getSingleStationMonthRainfall(String stcd, String TM) {
        //数据格式
        DecimalFormat df = new DecimalFormat("#0.0");
        // 日期格式转换
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        String endTm= null;
        String startTm=null;
        //最大返回对象
        MonthRainfall monthRainfall = new MonthRainfall();
        String start = TM+"-01-01 08:00:00";
        try {
            Date end =format.parse(start);
            calendar.setTime(end);
            calendar.add(Calendar.YEAR,1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String endtm =format.format(calendar.getTime());
        List<ST_PPTN_R> ST_PPTN_RLists = ST_PPTN_RService.selecStationMonthRainfall(stcd,start, endtm);
           monthRainfall.setUnit("mm");
                Map<String, List<ST_PPTN_R>> DayPPTNListGroup = ST_PPTN_RLists.stream().collect(Collectors.groupingBy(ST_PPTN_R::getMonth2, Collectors.toList()));
                for (Map.Entry<String, List<ST_PPTN_R>> DayPPTNList : DayPPTNListGroup.entrySet()) {
                    MonthData monthData = new MonthData();
                    monthData.setDay(DayPPTNList.getKey());
                    Map<String, List<ST_PPTN_R>> PPTNListGroup = DayPPTNList.getValue().stream().collect(Collectors.groupingBy(ST_PPTN_R::getMonth, Collectors.toList()));
                    for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
                        //根据月分组
                        //开始时间为 年+月+日 sethour8 结束为 年+月+日(+1) sethour8
                        startTm = PPTNList.getKey()+"-"+DayPPTNList.getKey()+" 08:00:00";
                        try {
                            Date dt = format.parse(startTm);
                            calendar.setTime(dt);
                            calendar.add(Calendar.HOUR,1);
                            calendar.add(Calendar.DATE,1);

                            List<ST_PPTN_R> s = ST_PPTN_RLists.stream().filter(ST_PPTN_R -> ST_PPTN_R.getDate().after(dt) && ST_PPTN_R.getDate().before(calendar.getTime()) && ST_PPTN_R.getDrp()!=null).collect(Collectors.toList());

                            double sum = s.stream().mapToDouble(ST_PPTN_R::getDrp).sum();
                            Data data = new Data(PPTNList.getKey(), sum);
                            monthData.setDatas(data);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

            }
                    if ( monthData.getDay()!=null&&monthData.getDatas()!=null){
                        monthRainfall.setMonthDatas(monthData);
                        monthData.getDatas().sort(Comparator.comparing(Data::getTM));
                    }
                   continue;
        }
            if (monthRainfall.getMonthDatas()!=null){
                monthRainfall.getMonthDatas().sort(Comparator.comparing(MonthData::getDay));
            }
        return monthRainfall;
    }

    //多站日报表
    //多站日逐时报表  多站雨量日报表
    public MultiStationRainfall getMultiStationDailyRainfall(String [] STCDS, String tm) {
        List<String> Stcds = Arrays.stream(STCDS).collect(Collectors.toList());

        //数据格式
        DecimalFormat df = new DecimalFormat("#0.0");
        // 日期格式转换
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        String endTm;
        String startTm;
        //最大返回对象
        MultiStationRainfall multiStationRainfalll = new MultiStationRainfall();
        //multiStationRainfalll.setPid(pid);
        multiStationRainfalll.setUnit("mm");
        List<String> stcds = new ArrayList<>();
        //调用返回站名站码方法
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bDMS_ST_SENSOR_BService.selectReportForms(Stcds);
        for (int i = 0; i < bdms_st_sensor_bs.size(); i++) {
            Data data = new Data();
            stcds.add(bdms_st_sensor_bs.get(i).getStcd());
            data.setStnm(bdms_st_sensor_bs.get(i).getStnm());
            data.setStcd(bdms_st_sensor_bs.get(i).getStcd());
            multiStationRainfalll.setStationInfos(data);
        }
        Data data1 = new Data();
        data1.setStnm("流域平均雨量");
        data1.setStcd("00000000");
        multiStationRainfalll.setStationInfos(data1);
        try {
            Date start = format.parse(tm + " 08:00:00");//开始日期
            startTm = format.format(start);
            calendar.setTime(start);
            calendar.add(Calendar.DATE, +1);
            endTm = format.format(calendar.getTime());

            List<ST_PPTN_R> List = ST_PPTN_RService.selecMultiStationDailyRainfall(Stcds, startTm, endTm);
            Map<String, List<ST_PPTN_R>> PPTNListGroup = List.stream().collect(Collectors.groupingBy(ST_PPTN_R::getData1, Collectors.toList()));
            for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
                //System.out.println(PPTNList.getKey());
                MultiStationRainfallData multiStationRainfallData = new MultiStationRainfallData();
                multiStationRainfallData.setTime( PPTNList.getKey());
                Data dataAvg = new Data();
                for (int i = 0; i < PPTNList.getValue().size(); i++) {
                    Data data = new Data();
                    data.setStcd(PPTNList.getValue().get(i).getStcd());
                    if (PPTNList.getValue().get(i).getDrp()!=null){
                        data.setValue(PPTNList.getValue().get(i).getDrp());
                        multiStationRainfallData.setDatas(data);
                        double sum = multiStationRainfallData.getDatas().stream().mapToDouble(Data::getValue).sum()+0.0;
                        Double avg = sum / bdms_st_sensor_bs.size();
                        dataAvg.setStcd("00000000");
                        dataAvg.setValue(Double.parseDouble(df.format(avg)));
                        multiStationRainfallData.setBasinRainfallAvg(dataAvg);
                    }

                }
                multiStationRainfalll.setMultiStationRainfallDatas(multiStationRainfallData);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return multiStationRainfalll;
    }

    //多站月逐日报表  多站雨量月报表
    public MultiStationRainfall getMultiStationMonthRainfall(String [] STCDS, String tm) {
        DecimalFormat df = new DecimalFormat("#0.0");
        List<String> Stcds = Arrays.stream(STCDS).collect(Collectors.toList());
        // 日期格式转换
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        //最大返回对象
        MultiStationRainfall multiStationRainfall = new MultiStationRainfall();
        multiStationRainfall.setUnit("mm");
        List<String> stcds=new ArrayList<>();
        //调用返回站名站码方法
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bDMS_ST_SENSOR_BService.selectReportForms(Stcds);
        System.out.println(bdms_st_sensor_bs.size());
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b : bdms_st_sensor_bs) {
            Data data = new Data();
            stcds.add(bdms_st_sensor_b.getStcd());
            data.setStnm(bdms_st_sensor_b.getStnm());
            data.setStcd(bdms_st_sensor_b.getStcd());
            multiStationRainfall.setStationInfos(data);
        }
        System.out.println(stcds.size());
        Data data1 = new Data();
        data1.setStnm("流域平均雨量");
        data1.setStcd("00000000");
        multiStationRainfall.setStationInfos(data1);
        ////调用返回实况数据方法
        String starttm = tm+"-01 08:00:00";
        try {
            Date end =format.parse(starttm);
            calendar.setTime(end);
            calendar.add(Calendar.MONTH,1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String endtm =format.format(calendar.getTime());

        List<ST_PPTN_R> ST_PPTN_RLists =  ST_PPTN_RService.selectMultiStationMonthRainfall(Stcds,starttm,endtm);
        //根据小时分组
        Map<String, List<ST_PPTN_R>> PPTNListGroup = ST_PPTN_RLists.stream().collect(Collectors.groupingBy(ST_PPTN_R::getMonth2, Collectors.toList()));
        for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
            //根据站码分组
                MultiStationRainfallData multiStationRainfallData = new MultiStationRainfallData();
                multiStationRainfallData.setTime(PPTNList.getKey());
                try {
                    Date start = format.parse(tm+"-"+PPTNList.getKey()+" 08:00:00");//开始日期
                    String startTm = format.format(start);
                    calendar.setTime(start);
                    calendar.add(Calendar.DATE ,+1);
                    String endTm = format.format(calendar.getTime());
                    Data dataAvg = new Data();
                    List<ST_PPTN_R> List = ST_PPTN_RService.selectSumMonthRainfall(stcds,startTm,endTm);

                    for (ST_PPTN_R st_pptn_r : List) {
                        Data data = new Data();
                        data.setStcd(st_pptn_r.getStcd());
                        if (st_pptn_r.getDrp() != null) {
                            data.setValue(st_pptn_r.getDrp());
                            multiStationRainfallData.setDatas(data);
                            Double sum = multiStationRainfallData.getDatas().stream().mapToDouble(Data::getValue).sum();
                            Double avg = sum / bdms_st_sensor_bs.size();
                            dataAvg.setStcd("00000000");
                            dataAvg.setValue(Double.parseDouble(df.format(avg)));
                            multiStationRainfallData.setBasinRainfallAvg(dataAvg);
                        }
                    }

                } catch (ParseException e) {
                    e.printStackTrace();
                }
                multiStationRainfall.setMultiStationRainfallDatas(multiStationRainfallData);

        }
        return multiStationRainfall;
    }

    //多站年逐月报表  多站年逐月报表 GetMultiStationYearRainfall
    public MultiStationRainfall getMultiStationYearRainfall(String [] STCDS, String tm) {
        List<String> Stcds = Arrays.stream(STCDS).collect(Collectors.toList());
        //数据格式
        DecimalFormat df = new DecimalFormat("#0.0");
        // 日期格式转换
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        String endTm= null;
        String startTm=null;
        MultiStationRainfall multiStationRainfall = new MultiStationRainfall();
        multiStationRainfall.setUnit("mm");
        List<String> stcds=new ArrayList<>();
        //调用返回站名站码方法
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bDMS_ST_SENSOR_BService.selectReportForms(Stcds);
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b : bdms_st_sensor_bs) {
            Data data = new Data();
            stcds.add(bdms_st_sensor_b.getStcd());
            data.setStnm(bdms_st_sensor_b.getStnm());
            data.setStcd(bdms_st_sensor_b.getStcd());
            multiStationRainfall.setStationInfos(data);
        }
        Data data1 = new Data();
        data1.setStnm("流域平均雨量");
        data1.setStcd("00000000");
        multiStationRainfall.setStationInfos(data1);
        ////调用返回实况数据方法
        String starttm =tm+"-01-01 08:00:00";
        try {
            Date end =format.parse(starttm);
            calendar.setTime(end);
            calendar.add(Calendar.YEAR,1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String endtm =format.format(calendar.getTime());

        List<ST_PPTN_R> ST_PPTN_RLists =  ST_PPTN_RService.selectMultiStationYearRainfall(stcds,starttm,endtm);
        //分组
        Map<String, List<ST_PPTN_R>> PPTNListGroup = ST_PPTN_RLists.stream().collect(Collectors.groupingBy(ST_PPTN_R::getMonth, Collectors.toList()));
        for (Map.Entry<String, List<ST_PPTN_R>> PPTNList : PPTNListGroup.entrySet()) {
            MultiStationRainfallData multiStationRainfalllData = new MultiStationRainfallData();
            multiStationRainfalllData.setTime(PPTNList.getKey().substring(5,7));
            try {
                Date start = format.parse(PPTNList.getKey()+"-01 08:00:00");//开始日期
                startTm =format.format(start);
                if (startTm.startsWith("12", 5)){
                    calendar.setTime(start);
                    calendar.add(Calendar.YEAR ,+1);
                    endTm=format.format(calendar.getTime());
                }else {
                    calendar.setTime(start);
                    calendar.add(Calendar.MONTH ,+1);
                    endTm=format.format(calendar.getTime());
                }
                Data dataAvg = new Data();
                List<ST_PPTN_R> List = ST_PPTN_RService.selectSumMonthRainfall(stcds,startTm,endTm);
                for (int i=0;i<List.size();i++){
                    Data data = new Data();
                    data.setStcd(List.get(i).getStcd());
                    if (List.get(i).getDrp()!=null){
                        data.setValue(List.get(i).getDrp());
                        multiStationRainfalllData.setDatas(data);
                        double sum = multiStationRainfalllData.getDatas().stream().mapToDouble(Data::getValue).sum();
                        Double avg = sum/bdms_st_sensor_bs.size();
                        dataAvg.setStcd("00000000");
                        dataAvg.setValue(Double.parseDouble(df.format(avg)));
                        multiStationRainfalllData.setBasinRainfallAvg(dataAvg);
                    }

                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            multiStationRainfall.setMultiStationRainfallDatas(multiStationRainfalllData);
        }

        return multiStationRainfall;
    }

    public MultiStationRainfall getMultiStationRainfall(String str1,int num1,String str2,MultiStationRainfall multiStationRainfall1){

        SimpleDateFormat format1 = new SimpleDateFormat(str1); //


        java.util.Date time = null;
        try {
            time = format1.parse(str2);//

        } catch (ParseException e) {
            e.printStackTrace();
        }
        MultiStationRainfall multiStationRainfall =new MultiStationRainfall();
        List<MultiStationRainfallData> multiStationRainfallDataList =new ArrayList<>();
                    if (num1==24){
                        for (int i=0;i<num1 ;i++) {//num1
                            MultiStationRainfallData multiStationRainfallData = new MultiStationRainfallData();
                            time.setHours(+i);
                            multiStationRainfallData.setTime(format1.format(time));
                            multiStationRainfallDataList.add(multiStationRainfallData);
                        }
                    }
                    if (num1==31){
                        for (int i=0;i<num1 ;i++) {//num1
                            MultiStationRainfallData multiStationRainfallData = new MultiStationRainfallData();
                            time.setDate(+i);
                            multiStationRainfallData.setTime(format1.format(time));
                            multiStationRainfallDataList.add(multiStationRainfallData);
                        }
                    }
                    if (num1==30){
                        for (int i=1;i<=num1 ;i++) {//num1
                            MultiStationRainfallData multiStationRainfallData = new MultiStationRainfallData();
                            time.setDate(+i);
                            multiStationRainfallData.setTime(format1.format(time));
                            multiStationRainfallDataList.add(multiStationRainfallData);
                        }
                    }
                    if (num1==29){
                        for (int i=0;i<num1 ;i++) {//num1
                            MultiStationRainfallData multiStationRainfallData = new MultiStationRainfallData();
                            time.setDate(+i);
                            multiStationRainfallData.setTime(format1.format(time));
                            multiStationRainfallDataList.add(multiStationRainfallData);
                        }
                    }
                    if (num1==28){
                        for (int i=0;i<num1 ;i++) {//num1
                            MultiStationRainfallData multiStationRainfallData = new MultiStationRainfallData();
                            time.setDate(+i);
                            multiStationRainfallData.setTime(format1.format(time));
                            multiStationRainfallDataList.add(multiStationRainfallData);
                        }
                    }
                    if (num1==12){
                        for (int i=0;i<num1 ;i++) {//num1
                            MultiStationRainfallData multiStationRainfallData = new MultiStationRainfallData();
                            time.setMonth(+i);
                            multiStationRainfallData.setTime(format1.format(time));
                            multiStationRainfallDataList.add(multiStationRainfallData);
                        }

                    }


        String a1=null;
        String b1=null;
        System.out.println(multiStationRainfallDataList.size());
        for (int a =0;a<multiStationRainfallDataList.size();a++){
            a1 = multiStationRainfallDataList.get(a).getTime();
            multiStationRainfall.setUnit(multiStationRainfall1.getUnit());
            multiStationRainfall.setPid(multiStationRainfall1.getPid());
            multiStationRainfall.setStationInfos(multiStationRainfall1.getStationInfos());
            if (multiStationRainfall1.getMultiStationRainfallDatas()!=null) {
                for (int b = 0; b < multiStationRainfall1.getMultiStationRainfallDatas().size(); b++) {

                    b1 = multiStationRainfall1.getMultiStationRainfallDatas().get(b).getTime();
                    if (Objects.equals(a1, b1)) {
                        multiStationRainfallDataList.get(a).setTime(multiStationRainfall1.getMultiStationRainfallDatas().get(b).getTime());
                        multiStationRainfallDataList.get(a).setBasinRainfallAvg(multiStationRainfall1.getMultiStationRainfallDatas().get(b).getBasinRainfallAvg());
                        multiStationRainfallDataList.get(a).setDatas(multiStationRainfall1.getMultiStationRainfallDatas().get(b).getDatas());
                        break;
                    }
                }
            }
            multiStationRainfall.setMultiStationRainfallDatas(multiStationRainfallDataList);
        }
        if (multiStationRainfall.getMultiStationRainfallDatas()!=null){
            multiStationRainfall.getMultiStationRainfallDatas().sort(Comparator.comparing(MultiStationRainfallData::getTime));
        }
        return multiStationRainfall;
    }

    //根据站码判断获取对应数据
    public String getSensorType(String stcd){

        BDMS_ST_SENSOR_B bdms_st_sensor_b=  bdms_st_sensor_bService.selectbyStcd(stcd);
        System.out.println(bdms_st_sensor_b);
        return bdms_st_sensor_b.getSensortps();

    }

    public String getSensorType2(String stcd){

        BDMS_ST_SENSOR_B bdms_st_sensor_b=  bdms_st_sensor_bService.selectStnm(stcd);
        System.out.println(bdms_st_sensor_b);
        return bdms_st_sensor_b.getSensortps();

    }
    //水位报表
    //水位日逐时报表
    public HourlyWaterLevelData getStationDailyWaterLevel(String stcd, String TM) {


        // 日期格式转换
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        //设置时间 2022-05-01 08:00:00
        String start = TM+"-01 08:00:00";
        try {
            Date end =format.parse(start);
            calendar.setTime(end);
            calendar.add(Calendar.MONTH,1);
            calendar.add(Calendar.MINUTE,-1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String endtm =format.format(calendar.getTime());
        HourlyWaterLevelData hourlyWaterLevelData = new HourlyWaterLevelData();
        //赋值
        //调用返回实况数据接口

        //判断数据什么类型

        switch (  getSensorType2(stcd)){
            case "Z":
                List<ST_RIVER_R> ST_RIVER_RLists = ST_RIVER_RService.selecStationDailyWaterLevel(stcd, start,endtm);
                Map<String, List<ST_RIVER_R>> RIVERListGroup = ST_RIVER_RLists.stream().collect(Collectors.groupingBy(ST_RIVER_R::getData1, Collectors.toList()));
                for (Map.Entry<String, List<ST_RIVER_R>> RIVERList : RIVERListGroup.entrySet()) {
                    boolean res = RIVERList.getKey().startsWith("00", 3);
                    if (res){
                        hourlyWaterLevelData.setUnit("m");
                        HourlyData hourlyData = new HourlyData();
                        for (int i = 0; i < RIVERList.getValue().size(); i++) {
                            hourlyWaterLevelData.setStationCode(RIVERList.getValue().get(i).getStcd());
                            Data data = new Data();
                            data.setTM(RIVERList.getValue().get(i).getData());
                            data.setValue(RIVERList.getValue().get(i).getZ());
                            hourlyData.setDatas(data);
                            hourlyData.setHour(RIVERList.getKey());
                        }
                        hourlyWaterLevelData.setHourlyDatas(hourlyData);
                    }
                    if (hourlyWaterLevelData.getHourlyDatas()!=null) {
                        hourlyWaterLevelData.getHourlyDatas().sort(Comparator.comparing(HourlyData::getHour));
                    }
                }
                break;
            case "RZ":
                List<ST_RSVR_R> ST_RSVR_RLists = ST_RSVR_RService.selecStationDailyWaterLevel(stcd, start,endtm);
                Map<String, List<ST_RSVR_R>> RSVRListGroup = ST_RSVR_RLists.stream().collect(Collectors.groupingBy(ST_RSVR_R::getData1, Collectors.toList()));
                for (Map.Entry<String, List<ST_RSVR_R>> RSRVRRList : RSVRListGroup.entrySet()) {
                    boolean res = RSRVRRList.getKey().startsWith("00", 3);
                    if (res){
                        hourlyWaterLevelData.setUnit("m");
                        HourlyData hourlyData = new HourlyData();
                        for (int i = 0; i < RSRVRRList.getValue().size(); i++) {
                            hourlyWaterLevelData.setStationCode(RSRVRRList.getValue().get(i).getStcd());
                            Data data = new Data();
                            data.setTM(RSRVRRList.getValue().get(i).getData());
                            data.setValue(RSRVRRList.getValue().get(i).getRz());
                            hourlyData.setDatas(data);
                            hourlyData.setHour(RSRVRRList.getKey());
                        }
                        hourlyWaterLevelData.setHourlyDatas(hourlyData);
                    }
                    if (hourlyWaterLevelData.getHourlyDatas()!=null) {
                        hourlyWaterLevelData.getHourlyDatas().sort(Comparator.comparing(HourlyData::getHour));
                    }
                }
                break;
            default:
                break;
        }


        return hourlyWaterLevelData;
    }

    //水位月逐日报表  GetStationMonthWaterLevel
    public MonthWaterLevel getStationMonthWaterLevel(String stcd, String TM) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 日期格式转换
        Calendar calendar = new GregorianCalendar();
        //设置时间 2022-05-01 08:00:00
        String start = TM+"-01-01 08:00:00";
        try {
            Date end =format.parse(start);
            calendar.setTime(end);
            calendar.add(Calendar.YEAR,1);
            calendar.add(Calendar.MINUTE,-1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String endtm =format.format(calendar.getTime());
        //最大返回对象
        MonthWaterLevel monthWaterLevel= new MonthWaterLevel();


        //判断类型
        switch (getSensorType2(stcd)){
            case "Z":
                List<ST_RIVER_R> ST_RIVER_RLists = ST_RIVER_RService.selecStationMonthWaterLevel(stcd, start,endtm);

                Map<String, List<ST_RIVER_R>> RIVERListGroup = ST_RIVER_RLists.stream().collect(Collectors.groupingBy(ST_RIVER_R::getMonth1, Collectors.toList()));
                for (Map.Entry<String, List<ST_RIVER_R>> RIVERList : RIVERListGroup.entrySet()) {
                    monthWaterLevel.setUnit("m");
                    MonthData monthData = new MonthData();
                    boolean res =  "".equals(RIVERList.getKey());
                    if (!res){
                    //天分组
                    monthData.setDay(RIVERList.getKey().substring(0,2));
                    Map<String, List<ST_RIVER_R>> MonthRIVERListGroup = RIVERList.getValue().stream().collect(Collectors.groupingBy(ST_RIVER_R::getMonth, Collectors.toList()));
                    for (Map.Entry<String, List<ST_RIVER_R>>  MonthRIVERList : MonthRIVERListGroup.entrySet()) {
                            for (int i = 0; i<MonthRIVERList.getValue().size(); i++){
                                monthWaterLevel.setStationCode(MonthRIVERList.getValue().get(i).getStcd());
                                if (MonthRIVERList.getValue().get(i)!=null) {
                                    Data data = new Data(MonthRIVERList.getValue().get(i).getMonth(), MonthRIVERList.getValue().get(i).getZ());
                                    monthData.setDatas(data);
                                    break;
                                }

                            }
                        }
                        if ( monthData.getDay()!=null){
                            monthWaterLevel.setMonthDatas(monthData);
                            monthData.getDatas().sort(Comparator.comparing(Data::getTM));
                        }

                    }
                }
                if ( monthWaterLevel.getMonthDatas()!=null){
                    monthWaterLevel.getMonthDatas().sort(Comparator.comparing(MonthData::getDay));
                }
                break;
            case "RZ":
                List<ST_RSVR_R> ST_RSVR_RLists = ST_RSVR_RService.selecStationMonthWaterLevel(stcd, start,endtm);

                Map<String, List<ST_RSVR_R>> RSVRListGroup = ST_RSVR_RLists.stream().collect(Collectors.groupingBy(ST_RSVR_R::getMonth1, Collectors.toList()));
                for (Map.Entry<String, List<ST_RSVR_R>> RIVERList : RSVRListGroup.entrySet()) {
                    monthWaterLevel.setUnit("m");
                    MonthData monthData = new MonthData();
                    boolean res =  RIVERList.getKey().equals("");
                    if (!res){
                        //天分组
                        monthData.setDay(RIVERList.getKey().substring(0,2));
                        Map<String, List<ST_RSVR_R>> MonthRIVERListGroup = RIVERList.getValue().stream().collect(Collectors.groupingBy(ST_RSVR_R::getMonth, Collectors.toList()));
                        for (Map.Entry<String, List<ST_RSVR_R>>  MonthRIVERList : MonthRIVERListGroup.entrySet()) {
                            for (int i = 0; i<MonthRIVERList.getValue().size(); i++){
                                monthWaterLevel.setStationCode(MonthRIVERList.getValue().get(i).getStcd());
                                if (MonthRIVERList.getValue().get(i)!=null) {
                                    Data data = new Data(MonthRIVERList.getValue().get(i).getMonth(), MonthRIVERList.getValue().get(i).getRz());
                                    monthData.setDatas(data);
                                    break;
                                }

                            }
                        }
                        if ( monthData.getDay()!=null){
                            monthWaterLevel.setMonthDatas(monthData);
                            monthData.getDatas().sort(Comparator.comparing(Data::getTM));
                        }

                    }
                }
                if ( monthWaterLevel.getMonthDatas()!=null){
                    monthWaterLevel.getMonthDatas().sort(Comparator.comparing(MonthData::getDay));
                }
                break;
            default:
                break;
        }

        return monthWaterLevel;
    }


    //获取实测潮位数据方法
    public List<TwcTide> getTide(List<String> stationCodes, String startTime, String endTime,Boolean hourData){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        List<TwcTide> twcTideList = new ArrayList<>();
        List<ST_TIDE_R> st_tide_rList =null;
        List<String> Stcds  = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> st_sensor_bs =  bdms_st_sensor_bService.selectByStcds(stationCodes);
        if (st_sensor_bs!=null&&st_sensor_bs.size()>0){
            for (BDMS_ST_SENSOR_B   st_sensor_b:st_sensor_bs){
                Stcds.add(st_sensor_b.getDatacode()) ;
            }
            if (hourData==null || hourData){
                st_tide_rList=  ST_TIDE_RService.selectByStcd(Stcds,startTime,endTime);
            }else{
                st_tide_rList  = ST_TIDE_RService.selectByStationArrys(Stcds,startTime,endTime);
            }


            if (st_tide_rList.size()>0){
                Map<String, List<ST_TIDE_R>> ListGroup = st_tide_rList.stream().collect(Collectors.groupingBy(ST_TIDE_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_TIDE_R>> List : ListGroup.entrySet()) {
                    List<TideData> dataList = new ArrayList<>();
                    BDMS_ST_SENSOR_B bdms_st_sensor_b = st_sensor_bs.stream().filter(ST_TIDE_R->ST_TIDE_R.getDatacode().equals(List.getKey())).findAny().orElse(null);
                    TwcTide twcTide = new TwcTide();
                    twcTide.setStationCode(bdms_st_sensor_b.getStcd());
                    for (int i=0;i<List.getValue().size();i++){
                        //  System.out.println(List.getValue().get(i));
                        TideData tideData = new TideData();
                        tideData.setTime(List.getValue().get(i).getTm());
                        tideData.setValue(List.getValue().get(i).getTdz());
                        tideData.setHLTDMK(String.valueOf(List.getValue().get(i).getHltdmk()));
                        tideData.setTDCHRCD(List.getValue().get(i).getTdchrcd());
                        tideData.setTDPTN(String.valueOf(List.getValue().get(i).getTdptn()));
                        tideData.setAirPressure(String.valueOf(List.getValue().get(i).getAirp()));
                        dataList.add(tideData);
                    }
                    twcTide.setDatas(dataList);
                    twcTideList.add(twcTide);
                }
            }else {
                List<ST_TIDE_R> st_tide_rs= ST_TIDE_RService.selectNewEstData(Stcds);
                System.out.println(st_tide_rs);
                for (String stcd: stationCodes){
                    TwcTide twcTide = new TwcTide();
                    List<TideData> dataList = new ArrayList<>();
                    BDMS_ST_SENSOR_B bdms_st_sensor_b = st_sensor_bs.stream().filter(ST_TIDE_R->ST_TIDE_R.getStcd().equals(stcd)).findAny().orElse(null);
                    ST_TIDE_R sttider= st_tide_rs.stream().filter(st_tide_r -> st_tide_r.getStcd().equals(bdms_st_sensor_b.getDatacode())).findAny().orElse(null);
                    twcTide.setStationCode(stcd);
                    if (sttider!=null){
//                    TideData tideData = new TideData();
//                    tideData.setTime(sttider.getTm());
//                    tideData.setValue(sttider.getTdz());
//                    tideData.setHLTDMK(String.valueOf(sttider.getHltdmk()));
//                    tideData.setTDCHRCD(sttider.getTdchrcd());
//                    tideData.setTDPTN(String.valueOf(sttider.getTdptn()));
//                    tideData.setAirPressure(String.valueOf(sttider.getAirp()));
//                    dataList.add(tideData);
                        try {
                            Date date = format.parse(sttider.getTm());
                            calendar.setTime(date);
                            calendar.add(Calendar.DATE,-3);
                            twcTide.setDataStartTime(format.format(calendar.getTime()));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                        twcTide.setDataEndTime(sttider.getTm().substring(0,19));
                        twcTide.setDatas(dataList);
                        twcTideList.add(twcTide);
                    }
                }
            }
        }

        return twcTideList;

    }


    //获取高低潮位数据方法
    public List<TwcTide> getHLTide(List<String> stationCodes, String startTime, String endTime){
        List<TwcTide> twcTideList = new ArrayList<>();

        List<ST_TIDE_R> st_tide_rList =null;

        List<String> Stcds  = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> st_sensor_bs =  bdms_st_sensor_bService.selectByStcds(stationCodes);
        if (st_sensor_bs!=null&&st_sensor_bs.size()>0){
            for (BDMS_ST_SENSOR_B   st_sensor_b:st_sensor_bs){
                Stcds.add(st_sensor_b.getDatacode()) ;

            }
            st_tide_rList  = ST_TIDE_RService.selectByStcd(Stcds,startTime,endTime);
            //根据站码分组
            Map<String, List<ST_TIDE_R>> ListGroup = st_tide_rList.stream().collect(Collectors.groupingBy(ST_TIDE_R::getStcd, Collectors.toList()));
            for (Map.Entry<String, List<ST_TIDE_R>> List : ListGroup.entrySet()) {
                BDMS_ST_SENSOR_B bdms_st_sensor_b = st_sensor_bs.stream().filter(ST_TIDE_R->ST_TIDE_R.getDatacode().equals(List.getKey())).findAny().orElse(null);
                TwcTide twcTide = new TwcTide();
                twcTide.setStationCode(bdms_st_sensor_b.getStcd());
                List<TideData> dataList = new ArrayList<>();
                for (int i=0;i<List.getValue().size();i++){
                    //  System.out.println(List.getValue().get(i));

                    if (List.getValue().get(i).getHltdmk()!=null){
                        TideData tideData = new TideData();
                        tideData.setTime(List.getValue().get(i).getTm());
                        tideData.setValue(List.getValue().get(i).getTdz());
                        tideData.setHLTDMK(String.valueOf(List.getValue().get(i).getHltdmk()));
                        tideData.setTDCHRCD(List.getValue().get(i).getTdchrcd());
                        tideData.setTDPTN(String.valueOf(List.getValue().get(i).getTdptn()));
                        tideData.setAirPressure(String.valueOf(List.getValue().get(i).getAirp()));
                        dataList.add(tideData);
                    }
                }
                twcTide.setDatas(dataList);
                twcTideList.add(twcTide);
            }
        }


        return twcTideList;
    }







    public List<String> getSensor(String STTP,List<SensorType> sensorTypeT) {
        List<String> sensor_T = new ArrayList<>();
        //如果STtp 不为空 senbuweikong
        if (STTP != null && !"".equals(STTP)) {
            if (sensorTypeT !=null && sensorTypeT.size() > 0) {
                for (SensorType sensorType : sensorTypeT) {
                    sensor_T.add(sensorType.toString());
                }
            } else {
                switch (STTP) {
                    case "PP":
                        sensor_T.add("P");
                        break;
                    case "ZQ":
                        sensor_T.addAll(Arrays.asList("P", "Z", "Q"));
                        break;
                    case "RR":
                        sensor_T.addAll(Arrays.asList("P", "INQ", "OTQ", "RZ", "W"));
                        break;
                    case "TDZ":
                        sensor_T.add("TDZ");
                        break;
                    case "DD":
                        sensor_T.addAll(Arrays.asList("UPZ", "DWZ", "TGTQ"));
                        break;
                    case "DP":
                        sensor_T.addAll(Arrays.asList("PPUPZ", "PPDWZ", "PMPQ"));
                        break;
                    case "Wind":
                        sensor_T.addAll(Arrays.asList("WNDV", "WNDDIR", "AD", "AC", "AE", "AF", "AFMX", "AEMX", "ABAVWVTM", "ABAVWV", "ABMX", "AAMX"));
                        break;
                    case "Wave":
                        sensor_T.addAll(Arrays.asList("AVWVHGT", "AVWVHGTCY", "WVDIR", "WVHGT0103", "WVHGT010", "MWVHGT"));
                        break;
                    case "TideWay":
                        sensor_T.addAll(Arrays.asList("V00H", "DIR00H", "V02H", "DIR02H", "V04H", "DIR04H", "V06H", "DIR06H", "V08H", "DIR08H", "V10H", "DIR10H", "AVV", "AVDIR"));
                        break;
                    case "SiltContent":
                        sensor_T.add("SED");
                        break;
                    case "TT":
                        sensor_T.add("TDZ");
                        break;
                    case "WQ":
                        sensor_T.addAll(Arrays.asList("TP", "TQ", "NH4", "ConMn", "PH", "WTMP", "TURB", "DOX","COND"));
                        break;
                    default:
                        break;
                }
            }
        }else if (sensorTypeT != null){
            for (SensorType type : sensorTypeT) {
                sensor_T.add(type.toString());
            }
        }





//        boolean res = sensorTypeT != null; // 传感器不为空
//
//        if (STTP != null && res) {// 站类不为空
//            switch (STTP) {
//                case "PP":
//                    sensor_T.add("P");
//                    break;
//                case "ZQ":
//                    sensor_T.add("P");
//                    sensor_T.add("Z");
//                    sensor_T.add("Q");
//                    break;
//                case "RR":
//                    sensor_T.add("P");
//                    sensor_T.add("INQ");
//                    sensor_T.add("OTQ");
//                    sensor_T.add("RZ");
//                    sensor_T.add("W");
//                    break;
//                case "TDZ":
//                    sensor_T.add("TDZ");
//                    break;
//                case "DD":
//                    sensor_T.add("UPZ");
//                    sensor_T.add("DWZ");
//                    sensor_T.add("TGTQ");
//                    break;
//                case "DP":
//                    sensor_T.add("PPUPZ");
//                    sensor_T.add("PPDWZ");
//                    sensor_T.add("PMPQ");
//                    break;
//                case "Wind":
//                    sensor_T.add("WNDV");
//                    sensor_T.add("WNDDIR");
//                    sensor_T.add("AD");
//                    sensor_T.add("AC");
//                    sensor_T.add("AE");
//                    sensor_T.add("AF");
//                    sensor_T.add("AFMX");
//                    sensor_T.add("AEMX");
//                    sensor_T.add("ABAVWVTM");
//                    sensor_T.add("ABAVWV");
//                    sensor_T.add("ABMX");
//                    sensor_T.add("AAMX");
//                    break;
//                //波浪表
//                case "Wave":
//                    sensor_T.add("AVWVHGT");
//                    sensor_T.add("AVWVHGTCY");
//                    sensor_T.add("WVDIR");
//                    sensor_T.add("WVHGT0103");
//                    sensor_T.add("WVHGT010");
//                    sensor_T.add("MWVHGT");
//                    break;
//                case "TideWay":
//                    sensor_T.add("V00H");
//                    sensor_T.add("DIR00H");
//                    sensor_T.add("V02H");
//                    sensor_T.add("DIR02H");
//                    sensor_T.add("V04H");
//                    sensor_T.add("DIR04H");
//                    sensor_T.add("V06H");
//                    sensor_T.add("DIR06H");
//                    sensor_T.add("V08H");
//                    sensor_T.add("DIR08H");
//                    sensor_T.add("V10H");
//                    sensor_T.add("DIR10H");
//                    sensor_T.add("AVV");
//                    sensor_T.add("AVDIR");
//                    break;
//                case "SiltContent":
//                    sensor_T.add("SED");
//                    break;
//                case "TideLevel":
//                    sensor_T.add("TDZ");
//                    break;
//                default:
//                    break;
//            }
//            for (SensorType type : sensorTypeT) {
//                sensor_T.add(type.toString());
//            }
//        } else if (res) { // 传感器不为空
//            for (SensorType type : sensorTypeT) {
//                sensor_T.add(type.toString());
//            }
//        } else if (STTP != null) {
//            switch (STTP) {
//                case "PP":
//                    sensor_T.add("P");
//                    break;
//                case "ZQ":
//                    sensor_T.add("P");
//                    sensor_T.add("Z");
//                    sensor_T.add("Q");
//                    break;
//                case "RR":
//                    sensor_T.add("P");
//                    sensor_T.add("INQ");
//                    sensor_T.add("OTQ");
//                    sensor_T.add("RZ");
//                    sensor_T.add("W");
//                    break;
//                case "TDZ":
//                    sensor_T.add("TDZ");
//                    break;
//                case "DD":
//                    sensor_T.add("UPZ");
//                    sensor_T.add("DWZ");
//                    sensor_T.add("TGTQ");
//                    break;
//                case "DP":
//                    sensor_T.add("PPUPZ");
//                    sensor_T.add("PPDWZ");
//                    sensor_T.add("PMPQ");
//                    break;
//                case "Wind":
//                    sensor_T.add("WNDV");
//                    sensor_T.add("WNDDIR");
//                    sensor_T.add("AD");
//                    sensor_T.add("AC");
//                    sensor_T.add("AE");
//                    sensor_T.add("AF");
//                    sensor_T.add("AFMX");
//                    sensor_T.add("AEMX");
//                    sensor_T.add("ABAVWVTM");
//                    sensor_T.add("ABAVWV");
//                    sensor_T.add("ABMX");
//                    sensor_T.add("AAMX");
//                    break;
//                //波浪表
//                case "Wave":
//                    sensor_T.add("AVWVHGT");
//                    sensor_T.add("AVWVHGTCY");
//                    sensor_T.add("WVDIR");
//                    sensor_T.add("WVHGT0103");
//                    sensor_T.add("WVHGT010");
//                    sensor_T.add("MWVHGT");
//                    break;
//                case "TideWay":
//                    sensor_T.add("V00H");
//                    sensor_T.add("DIR00H");
//                    sensor_T.add("V02H");
//                    sensor_T.add("DIR02H");
//                    sensor_T.add("V04H");
//                    sensor_T.add("DIR04H");
//                    sensor_T.add("V06H");
//                    sensor_T.add("DIR06H");
//                    sensor_T.add("V08H");
//                    sensor_T.add("DIR08H");
//                    sensor_T.add("V10H");
//                    sensor_T.add("DIR10H");
//                    sensor_T.add("AVV");
//                    sensor_T.add("AVDIR");
//                    break;
//                case "SiltContent":
//                    sensor_T.add("SED");
//                    break;
//                case "TT":
//                    sensor_T.add("TDZ");
//                    break;
//
//
//            }
        //}
        return  sensor_T.stream().distinct().collect(Collectors.toList());
    }
}

